0

我正在尝试使用来自另一个模型的列作为外键来扩展用户配置文件。但是,在保存其抛出的错误消息的同时。

Models.py:

class Team_Profile(models.Model):
    team_name = models.CharField(max_length=200, unique=True)
    
    
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    team_name = models.ForeignKey(Team_Profile)


    def __str__(self):
        return "%s's profile" % self.user

    def save(self, *args, **kwargs):
        try:
            existing = UserProfile.objects.get(user=self.user)
            self.id = existing.id
        except UserProfile.DoesNotExist:
            pass
        models.Model.save(self, *args, **kwargs)

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)

下面是我的views.py:

视图.py:

data = request.POST.copy()
team = data['team_hidden']
try:
            team_name = Team_Profile.objects.get(team_name=team)
except Team_Profile.DoesNotExist:
            team_profile = Team_Profile()
            team_profile.team_name = team
            team_profile.save()
                
try:
            user=User.objects.get(username=name)
            return render_to_response("user_exist.html", {'media_url':media_url})
            
except User.DoesNotExist:

            user= User.objects.create_user(name,User.email,User.password)
            user.first_name=first_name
            user.last_name=last_name
            user.save()

                        
            userprofile = UserProfile()
            userprofile.user = user
            userprofile.team_name = Team_Profile.objects.get(team_name=team)                    


            userprofile.save()

如果您需要更多信息,请告诉我。任何帮助表示赞赏。

追溯:

追溯:

File "/home/dtechsun/testapp/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/dtechsun/testapp/myentrancetest/views.py" in create_login
  796.          user= User.objects.create_user(name,User.email,User.password)
File "/home/dtechsun/testapp/django/contrib/auth/models.py" in create_user
  135.         user.save(using=self._db)
File "/home/dtechsun/testapp/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/dtechsun/testapp/django/db/models/base.py" in save_base
  592.                 created=created, raw=raw, using=using)
File "/home/dtechsun/testapp/django/dispatch/dispatcher.py" in send
  172.             response = receiver(signal=self, sender=sender, **named)
File "/home/dtechsun/testapp/myentrancetest/models.py" in create_user_profile
  53.         created = UserProfile.objects.get_or_create(user=instance)
File "/home/dtechsun/testapp/django/db/models/manager.py" in get_or_create
  135.         return self.get_query_set().get_or_create(**kwargs)
File "/home/dtechsun/testapp/django/db/models/query.py" in get_or_create
  387.                 obj.save(force_insert=True, using=self.db)
File "/home/dtechsun/testapp/myentrancetest/models.py" in save
  49.       models.Model.save(self, *args, **kwargs)
File "/home/dtechsun/testapp/django/db/models/base.py" in save
  460.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/home/dtechsun/testapp/django/db/models/base.py" in save_base
  571.                     result = manager._insert(values, return_id=update_pk, using=using)
File "/home/dtechsun/testapp/django/db/models/manager.py" in _insert
  195.         return insert_query(self.model, values, **kwargs)
File "/home/dtechsun/testapp/django/db/models/query.py" in insert_query
  1437.     return query.get_compiler(using=using).execute_sql(return_id)
File "/home/dtechsun/testapp/dbindexer/compiler.py" in execute_sql
  256.         return super(SQLInsertCompiler, self).execute_sql(return_id=return_id)
File "/home/dtechsun/testapp/djangotoolbox/db/basecompiler.py" in execute_sql
  348.                                         "field) to None!" % field.name)

Exception Type: DatabaseError at /create_login/
Exception Value: You can't set team_name (a non-nullable field) to None!

谢谢,

苏尼尔

4

5 回答 5

1

为 team_name 添加默认值效果很好。感谢大家的帮助。

class UserProfile(models.Model): 
        user = models.OneToOneField(User)   
        team_name = models.ForeignKey(Team_Profile, default=0) 
于 2012-05-18T08:05:57.593 回答
1

你的整体逻辑似乎是正确的。但是,我认为错误似乎发生在第一个 except 块中

team_profile.team_name = team
我认为,此时团队将变为无,这将在下一行保存时引发错误。因此,请检查您从请求中获得的值。

于 2012-05-15T14:26:12.533 回答
0

可能是因为:

team = data['team_hidden']

团队可能是空的(在发布期间没有发送 team_hidden,或者它是一个获取请求......)

于 2012-05-15T13:51:54.730 回答
0

我相信这里的问题在于您将 UserProfile 作为 OneToOneField 映射到 User 模型。Django 期望 team_name 有一个非空值,因为它与 UserProfile 模型有关系。在创建用户时尝试设置 team_name,看看是否有帮助。

于 2012-05-15T13:14:18.467 回答
0

User.objects.create_user将创建/保存一个User实例,从而触发create_user_profile执行。然后当没有匹配的UserProfile实例时它会失败:

def create_user_profile(sender, instance, created, **kwargs):
    if created:
        # team_name is None, 
        # but UserProfile.team_name is declared w/ null=False, by default
        created = UserProfile.objects.get_or_create(user=instance)
于 2012-05-15T13:45:57.950 回答