我正在尝试使用来自另一个模型的列作为外键来扩展用户配置文件。但是,在保存其抛出的错误消息的同时。
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!
谢谢,
苏尼尔