我有一个与用户相距甚远的模式。 但是,当我根据这一模式提交表格时,我并没有正确地向用户提供。
class Thing(models.Model):
"""Things."""
user = models.ForeignKey(User)
salt = models.IntegerField()
# [...]
class GenerateThingForm(ModelForm):
"""A form for registering a new Thing."""
class Meta:
model = Thing
fields = (...) # There are some, but it would distract here
这一观点说:
@login_required()
def register_thing(request):
"""Register Thing."""
if request.method == POST : # If the form has been submitted...
form = GenerateThingForm(request.POST)
if form.is_valid():
form.salt = random.getrandbits(32)
# Apparently I need to specify form.user.
# Same result if just request.user
form.user = User.objects.get(request.user)
form.save() # Oh, no! user_id is null!
感谢任何倾斜。