I want to extend the User model with the following:
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)
So when I try to sync the db, it complains about category_id cannot be null, and the source of the problem is “create_client_profile”. How can I solve this?
If I remove “create_client_profile” it works but yes I will loose get_profile().
What do you think of this as a solution? I am not sure it is Djangoish enough 🙂
So, you’ve got a signal that automatically creates a Client whenever a User is created. Your Client model has an obligatory ForeignKey to Category, but your signal doesn’t set a category.
The solution should be obvious: either make your category FK
null=True, or get your signal to get/create the relevant category.