我收到以下错误:
Cannot assign "<django.db.models.fields.related.ForeignKey>": "Worry.user" must be a "User" instance.
我试图将当前用户的 id 分配给我刚刚创建的对象。
这是我的models.py的一部分:
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.db import models
class UserForm (ModelForm) :
class Meta:
model = User
class Worry(models.Model) :
user = models.ForeignKey(User) #Many worries to 1 user relation
This is part of my views.py:
from django.db import models
from django.shortcuts import render_to_response, redirect, get_object_or_404
from django.template import RequestContext
from django.contrib.auth.models import User
from holaProy.models import UserForm, Worry, WorryForm, Statistics, StatisticsForm
def worry_add (request):
form = WorryForm (request.POST or None)
if form.is_valid():
wform.user = models.ForeignKey('User') #HERE IS THE PROBLEM I THINK
wform.save()
return redirect (index)
return render_to_response ('holaProy/worry_add.html', {'worry_form': form}, context_instance = RequestContext(request))</code>
我应该如何做才能成功地将当前用户 ID 分配给实际担忧实例的“用户”字段?