Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何正确创建;“用户额外信息”;反对“完整性错误”;列*something*不能为空";_Django_Django Models_Django Users - Fatal编程技术网

Django 如何正确创建;“用户额外信息”;反对“完整性错误”;列*something*不能为空";

Django 如何正确创建;“用户额外信息”;反对“完整性错误”;列*something*不能为空";,django,django-models,django-users,Django,Django Models,Django Users,为了向我的auth.User模型中添加信息,我遵循以下步骤。 我想把一个用户链接到一个社会(假设一个用户是一个客户)和这个社会中的一份工作。 这是我的代码: 社团/models.py: from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save class UserProfile(models.Model):

为了向我的auth.User模型中添加信息,我遵循以下步骤。 我想把一个用户链接到一个社会(假设一个用户是一个客户)和这个社会中的一份工作。 这是我的代码:

社团/models.py:

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save


class UserProfile(models.Model):
    """
    Stores the user informations except
    login basic informations.
    """
    user = models.OneToOneField(User)
    firstname = models.CharField(max_length=128)
    lastname = models.CharField(max_length=128)
    society = models.ForeignKey('Society')
    job = models.ForeignKey('UserJob')


class Society(models.Model):
    """
    Stores the informations about the societies.
    """
    name = models.CharField(max_length=128)


class UserJob(models.Model):
    """
    Stores the user job category in the society.
    """
    name = models.CharField(max_length=64)


def create_user_profile(sender, instance, created, **kwargs):
    """
    Uses the post_save signal to link the User saving to
    the UserProfile saving.
    """
    if created:
        UserProfile.objects.create(user=instance)


#the instruction needed to use the post_save signal
post_save.connect(create_user_profile, sender=User)
社团/admin.py:

from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from societies.models import UserProfile, Society, UserJob


class UserProfileInline(admin.StackedInline):
    """
    Defines an inline admin descriptor for UserProfile model
    which acts a bit like a singleton
    """
    model = UserProfile
    can_delete = False
    verbose_name_plural = 'profile'


class UserAdmin(UserAdmin):
    """
    Defines a new User admin.
    """
    inlines = (UserProfileInline, )

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

admin.site.register(Society)
admin.site.register(UserJob)
我添加了UserProfileInline,以便在我的管理站点用户表单上具有这些字段。 我已将此行添加到my settings.py:

AUTH_PROFILE_MODULE = 'societies.UserProfile'
问题是,当我试图通过管理站点上的用户表单创建用户时,包括填写UserProfile特定字段,我得到以下结果:

IntegrityError位于/admin/auth/user/add/

(1048,“协会id”列不能为空)

因为我在表单上指定了一个社团,所以我在问自己,问题是否不是来自create_user_profile函数的信号处理。考虑到我前面提到的医生,没有更多的事情要做。但是,我不需要通过UserProfile.create(…)调用精确地填写UserProfile字段firstname、lastname、society和job吗?(除了“user=instance”参数之外)。在这种情况下,我不知道如何获得正确的元素来填充参数。在文件中,关于“被接受的动物”和“最喜欢的动物”没有做任何事情,所以我肯定错了。。。不是吗

非常感谢您的回复。
我为我的语言道歉。

我发现了我的错误,我必须在UserProfile模型中的Society和UserJob foreignkey字段中添加一个默认值。另一个解决方案是指定它们可以为null

很抱歉没有引起注意