保存时 Django 用户配置文件内联创建完整性错误
目前,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,学校的选择没有被通过,在这里也没有被通过:
您只是设置一个用户,并尝试创建配置文件。
将
null=True, Blank=True
添加到 school 字段,或找出学校选择是什么,并将其传递给:Yes, the choice of school isn't getting passed, it's not getting passed here:
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: