我想使用以下内容扩展 User 模型:
class Client(models.Model):
business_name = models.CharField(max_length=128)
category = models.ForeignKey(Category) # the beginning of the problem
user = models.ForeignKey(User, unique=True)
class Category(models.Model):
category = models.CharField(max_length=75)
#the problem is here
def create_client_profile(sender, instance, created, **kwargs):
if created:
Client.objects.create(user=instance)
post_save.connect(create_client_profile, sender=User)
所以当我尝试同步数据库时,它抱怨 category_id 不能为空,问题的根源是“create_client_profile”。我该如何解决这个问题?如果我删除“create_client_profile”,它会起作用,但是是的,我会丢失 get_profile()。
您如何看待这个解决方案?我不确定它是否足够 Djangoish :)