Search code examples
pythondjangodatabasedjango-orm

Django add fields to createsuperuser command


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:\Users\Project>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?


Solution

  • 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....)