python - Make `createsuperuser` work with custom / non-trivial user model -
in "create super user" step of python manage.py syncdb
, i'd able specify
- password
- first name
- middle name (blank acceptable)
- last name
- phone
my "user model" is:
class account(auth_models.abstractbaseuser): email = models.emailfield(unique = true, db_index = true) created_on = models.datefield(auto_now_add = true) person = models.foreignkey(person) def get_full_name(self): ... def get_short_name(self): ... objects = accountmanager() username_field = 'email' required_fields = []
and person
model is:
class person(models.model): name = models.charfield(max_length = 256) is_staff = models.booleanfield(default = false) phone = models.charfield(max_length = 16, blank = true)
my custom account "manager" naturally needs have these informations, did was:
def create_user(self, email, password, name, phone): normalized_email = self.normalize_email(email) person = person( name = name, phone = phone ) person.save() account = account(email = normalized_email, person = person) account.set_password(password) account.save() return account
at creation of course asks me email , password while ignoring person
model needs populated.
how can make 6 fields required in "create super user" step?
to going question idea of having account
, person
separated: single person
can have multiple account
s associated it; it's called "one-to-many relationship" between person
, account
; it's legal, check lawyer.
i tried go adding person
required_fields
that, specified in documentation:
since there no way pass model instances during createsuperuser prompt, expect user enter value of to_field value (the primary_key default) of existing instance.
requires person
id don't have (i need create person first).
specifying:
required_fields = ['person.name', ...]
is not supported (the error says person.name
not field of account
).
you need method called create_superuser()
should this:
def create_superuser(self, email, password, name, phone): user = self.create_user( email=email, password=password, name = name, phone = phone ) user.save(using=self._db) return user
this way required_fields = ['person']
works me.
Comments
Post a Comment