簡體   English   中英

NOT NULL約束失敗

[英]NOT NULL constraint failed

有人可以向我解釋我的錯誤嗎?

我想發送個人資料用戶表格時收到此錯誤

NOT NULL constraint failed: userprofile_userprofile.godzina_id

看起來像這樣:

我有一個應用“用戶個人資料”

django的forms.py從模型導入表單

class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile
        fields = ('imie', 'godzina')

views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.core.context_processors import csrf
from forms import UserProfileForm
from django.contrib.auth.decorators import login_required
from django.conf import settings

@login_required
def user_profile(request):
    if request.method == 'POST':
        form = UserProfileForm(request.POST, instance=request.user.profile)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/accounts/loggedin')
    else:
        user = request.user
        profile = user.profile
        form = UserProfileForm(instance=profile)
        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render(request, 'user_profile.html', args)

models.py:

from django.db import models
from django.contrib.auth.models import User
from godzina.models import Godzina

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    imie = models.CharField(max_length=150)
    godzina = models.ForeignKey('godzina.Godzina')   


User.profile = property(lambda u:UserProfile.objects.get_or_create(user=u)[0])

您可以將null=True添加到您的godzina屬性:

godzina = models.ForeignKey('godzina.Godzina', null=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM