I have 2 models Account and User. An Account can have multiple users.
When the first User(Owner) is created at the same time the Account will be created.
Because User have a ForeignKey to Account, I need first to create the Account, and after the User.
But Account had a field created_by which is request.user. So is a circular problem.
I think I need to create first the Account and created_by to be a superuser(first) and than create the User, and update the Account with the new user.
class Account(MetaData):
name = models.CharField(max_length=255)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True,
related_name='%(app_label)s_%(class)s_created_by', on_delete=models.CASCADE)
class User(AbstractBaseUser):
account = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name='owner')
How can I do that ?
I need this only first time, for the first user, because after that for the other users the Account will exist.
When I'm saying first user, I'm not referring to staff users (as superusers), but normal users for which an account is mandatory thru a register form.
blank=True is just for staff members.
accountfield onUsermodel is pointing toUser(notAccount) assumingAUTH_USER_MODELisUser.