So I know I can use signals to automatically create a one-to-one related instance. (For example: Create OneToOne instance on model creation).
My situation is that the related model contains a non-null, non-blank field.
class Company(models.Model):
name = models.CharField()
class UserProfile(models.Model):
user = models.OneToOneField( settings.AUTH_USER_MODEL )
company = models.ForeignKey( Company )
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_user_profile( sender, instance, created, **kwargs ):
if created:
UserProfile.objects.create(user=instance)
When a user is created, create_user_profile
is triggered. But this causes an error Column company_id cannot be null
. How do I pass company_id
into the receiver? Would I need to create a custom manager in the User
model? If so, would that just remove the need for signals since I can create UserProfile
in the manager?