Django add fields to createsuperuser command

Question:

I have several database tables that I use in Django. Now I want to design everything around the database not in the Django ORM but in the rational style of my MySQL database. This includes multiple tables for different information. I have made a drawing of what I mean. I want the createsuperuser command to query once for the user table and once for the residence table.

This could then look like this:

C:UsersProject>python manage.py createsuperuser
username: input
firstname: input
lastname: input
password: *****
country: input
city: input
street: input

enter image description here

Edit

Is there maybe a way to change the command?

Asked By: Henrik

||

Answers:

https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/#module-django.core.management

You can create django custom commands which will looks like:

python manage.py createsuperuser2 (or similar name)

class Command(BaseCommand):

    def handle(self, *args, **options):
       username = input("username")
       .... (next params)


       user = User.objects.get_or_create(username=username, defaults={"blabla"}
       Residence.objects.create(user=user, next params....)
Answered By: Linum

Just setup REQUIRED_FIELDS inside your User class. If you don’t have custom user class, create class User(AbstractUser) but dont forget to set AUTH_USER_MODEL = "users.User" in settings.py

REQUIRED_FIELDS = ["country","city","street"]
Answered By: slavny_coder
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.