出现错误“Todos.user”必须是“UserProfile”实例。有人可以解释为什么吗?我希望 Todos 中的用户应该指向 UserProfile 并且我在 Todos 中保存的任何内容都应该显示在 /profile/ 中?
class UserProfile(models.Model):
user = models.OneToOneField(User)
birth =models.DateField()
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Todos(models.Model):
user = models.ForeignKey(UserProfile)
title = models.CharField(max_length=100)
created = models.DateField()
start_time = models.TimeField()
end_time = models.TimeField()
def __unicode__(self):
return unicode(self.user)
形式
class todosform(ModelForm):
title = forms.CharField(label=(u'Todo'))
created = forms.DateField(label=(u'Date'))
start_time = forms.TimeField(label=(u'Start Time'))
end_time = forms.TimeField(label=(u'End Time'))
#user = forms.CharField(label=(u'username')
class Meta:
model = Todos
exclude=('user',)
#url todo url(r'^todo/$', 'registration.views.todo'),
def todo(request):
if request.user.is_authenticated():
Todos.objects.filter(user=request.user)
if request.method == 'POST':
form =formtodos(request.POST)
if form.is_valid():# All validation rules pass
todoss = form.save(commit=False)
todoss.user = request.user
form.save()
return HttpResponseRedirect('/profile/')
else:
form = formtodos()
context = {'form':form}
return render_to_response('todo.html', context, context_instance=RequestContext(request))
#url profile url(r'^profile/$', 'registration.views.Profile'),
@login_required # decorator to check if request login
def Profile(request):
if not request.user.is_authenticated(): #if user not logged in
return HttpResponseRedirect('/login/')
#model = request.user.todos_set.all().order_by('created')[:7]
#u = Todos.objects.filter(created_by = request.user).get(pk=user)
registration = request.user.get_profile
context = {'registration':registration }
return render_to_response('profile.html',context,context_instance=RequestContext(request))