我很确定这个问题已经被涵盖了,但是我尝试了我在堆栈中找到的所有相关内容来解决我的问题,但没有任何成功。
这是我的model.py
from django.db import models
from django.contrib.auth.models import User
class Description(models.Model):
titre_routine = models.CharField(max_length=100)
type_routine = models.CharField(max_length=100)
lieu_routine = models.CharField(max_length=100)
periode = models.CharField(max_length=100, blank=True)
routine_description = models.TextField()
raison_chgmt = models.TextField(blank=True)
user = models.ForeignKey(User)
#prendre une liste de routine et faire une liste de choix
#si non ajout a la liste de routine dans nouvelle table
def __unicode__(self):
return self.titre_routine
#User.profile = property(lambda u: Description.objects.get_or_create(user=u)[0])
class Rewards(models.Model):
jour = models.CharField(max_length=4)
activite_altr = models.TextField()
first_word = models.CharField(max_length=100, blank=True)
second_word = models.CharField(max_length=100, blank=True)
third_word = models.CharField(max_length=100, blank=True)
urge_validator = models.BooleanField() #urge satisfait ou non?
urge_validate_date = models.DateField(auto_now=True)
description = models.ForeignKey(Description)
def __unicode__(self):
return self.jour
解释我的模型:一个“用户”可以创建多个“描述”。一个“描述”包含多个“奖励”。...一个“描述”包含多个“其他事物”
这是我的观点.py
#the user access to his dashboard, dashboard filtered by the user logged in.
@login_required(login_url='/userauth/login/')
def dashboard(request):
routine_info = Description.objects.filter(user=request.user)
return render_to_response('dashboard.html', {
'routine_info': routine_info
})
#dashboard for the reward section that should be like the first dashboard filtered only by the user logged in AND should only show the reward related to One description created previously by the user.
@login_required(login_url='/userauth/login/')
def reward_dashboard(request):
reward_info = Rewards.objects.all()
return render_to_response('rewards_dashboard.html', {
'reward_info': reward_info
})
#the user can have access to the reward he created as the other def, he should be the only one to have access (again reward created by the user and related to ONE description)
@login_required(login_url='/userauth/login/')
def reward_description(request, reward_id):
reward_info = Rewards.objects.get(id=reward_id)
return render_to_response('reward_description.html', {
'reward': reward_info
})
#rewardform, I miss the pieces to obtain de description id that will link both table Rewards and Description (and recursively the User table via Description???)
@login_required(login_url='/userauth/login/')
def new_reward(request):
if request.POST:
form = RewardsForm(request.POST)
if form.is_valid():
obj = form.save(commit= False)
obj.description = #Missing piece
#test that could never happen
if obj.description == 200:
return HttpResponseRedirect('/reward_dashboard/')
else:
obj.save()
return HttpResponseRedirect('/reward_dashboard/')
else:
form = RewardsForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('create_reward.html', args)
概括一下:
- 用户可以访问他创建的奖励,因为他应该是唯一可以访问的人(再次奖励由用户创建并与一个描述相关)
- 奖励部分的仪表板应具有仅由登录用户过滤的第一个仪表板,并且应仅显示先前创建的与奖励相关的一个描述。
-rewardform,我错过了获取de description id的部分,它将链接表Rewards和Description(并通过Description递归地连接User表)
我应该尝试查看会话吗?(当用户创建描述时,我为他分配了一个特定的 ID?)
我只是希望我在解释中清楚地说明了我的情况。
感谢您的帮助!