保存时 Django 用户配置文件内联创建完整性错误

发布于 2024-11-25 00:59:45 字数 1292 浏览 0 评论 0原文

目前,我在 Django 中的用户配置文件方面遇到一些问题。

我按照建议创建了一个用户配置文件:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    is_teacher = models.BooleanField(verbose_name = "Teacher", help_text = "Designates whether this user is a teacher and can administer and add students and classes")
    school = models.ForeignKey(School)

    def __unicode__(self):
        return self.user.username

    def save(self, *args, **kwargs):
        if not self.pk:
            try:
               p = UserProfile.objects.get(user=self.user)
               self.pk = p.pk
           except UserProfile.DoesNotExist:
               pass

        super(UserProfile, self).save(*args, **kwargs)

我还将这个用户配置文件内嵌到我的用户管理页面中。这就是我用来确保用户配置文件与用户一起创建的方法:

def create_user_profile(sender, instance, created, **kwargs):
"""Create the UserProfile when a new User is saved"""
    if created:
        print "User Profile Creation: ", created
        UserProfile.objects.get_or_create(user=instance)

当我去添加新用户时,就会出现问题。在添加学校字段之前,我能够填写所有内容,并且效果很好。添加 school 字段后,我收到一个 IntegrityError,指出 userprofile.school_id 不能为 null。我这里哪里出错了?

我认为问题在于学校的选择没有传递给 get_or_create ,因此当它创建 UserProfile 时,它​​什么也看不到。那么,当单击 is_teacher 的复选框时,它之前是如何工作的呢?另外,我该如何解决这个问题?

I'm having some issues with user profiles in Django currently.

I followed the recommendations and created a user profile:

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    is_teacher = models.BooleanField(verbose_name = "Teacher", help_text = "Designates whether this user is a teacher and can administer and add students and classes")
    school = models.ForeignKey(School)

    def __unicode__(self):
        return self.user.username

    def save(self, *args, **kwargs):
        if not self.pk:
            try:
               p = UserProfile.objects.get(user=self.user)
               self.pk = p.pk
           except UserProfile.DoesNotExist:
               pass

        super(UserProfile, self).save(*args, **kwargs)

I also inlined this user profile into my User admin page. This is what I use to make sure that a user profile is created along with a user:

def create_user_profile(sender, instance, created, **kwargs):
"""Create the UserProfile when a new User is saved"""
    if created:
        print "User Profile Creation: ", created
        UserProfile.objects.get_or_create(user=instance)

The issue arises when I go to add a new user. Before adding the school field, I was able to fill everything in and it would work great. After adding the school field, I get an IntegrityError that states that userprofile.school_id cannot be null. Where am I going wrong here?

I believe the issue is that the choice of school is not being passed to the get_or_create and so when it goes to create the UserProfile, it sees nothing. How then did it work before, when clicking the checkbox for is_teacher? Also, how would I go about fixing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰沐子 2024-12-02 00:59:45

是的,学校的选择没有被通过,在这里也没有被通过:

 UserProfile.objects.get_or_create(user=instance)

您只是设置一个用户,并尝试创建配置文件。

null=True, Blank=True 添加到 school 字段,或找出学校选择是什么,并将其传递给:

 UserProfile.objects.get_or_create(user=instance, school=school)

Yes, the choice of school isn't getting passed, it's not getting passed here:

 UserProfile.objects.get_or_create(user=instance)

You're only setting a user, and trying to create the profile.

Add null=True, blank=True to the school field, or figure out what the school choice was, and pass that in:

 UserProfile.objects.get_or_create(user=instance, school=school)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文