[PYTHON] django makemigarations createsuperuser

Then, I wrote it last in the models I created last time.

models.py


def __str__(selef):
   return selef.name

This is the part, but I still don't fully understand it. When each record is displayed in a list on the management screen, It is difficult to understand if the IDs are lined up, so I think that the employee name is displayed instead.

def __init__ 

Somehow it's still chilling ...

I want to learn while creating.

I try to get to the end with a little combination of knowledge, so There may be many things that you don't know about useful features.

Now, let's create a table for the staff model.

tarminal


python3 manage.py makemigrations

tarminal


Migrations for 'staff':
  staff/migrations/0001_initial.py
    - Create model Staff

Does this make you feel like your first staff table is ready to be created? Since it is displayed as create, I feel that it is made, but It seems that the model is created for the first time with the following instruction.

I will issue another command from here.

terminal


python3 manage.py migrate

image.png

When you migrate for the first time, various things will be created automatically. Is user information about user, which is a useful feature in django? I would like someone with knowledge to teach me about this.

Next, create a superuser, which is necessary to view and create data on the management screen.

terminal


python3 manage.py createsuperuser

Do this Set a user name and password to complete the creation

image.png

Add the following to admin.py so that you can handle the table on the admin screen

admin.py


from django.contrib import admin
from .models import Staff


admin.site.register(Staff)

The table will now be displayed on the management screen. Now, let's start the server and try to enter the management screen.

terminal


pythone manage.py runserver

Put http://127.0.0.1:8000/admin in the url and display it with a web browser.

image.png

It was displayed properly. It was good

Try logging in with the administrator you created earlier.

image.png

I put it in! Was good

But the staff table is not displayed ... You may have forgotten to save admin.py. Save and rerun ...

image.png

Displayed: thumbs up:

Why is it displayed in staffs?

I tried to open the screen to actually register the data

image.png

Ah, the date of leaving the company may not be the same as the date of joining the company, but it is set to the same as the date of joining the company. Modify the models and migrate again.

python:staff.models.py


class Staff(models.Model):
    id = models.AutoField(verbose_name='Employee ID',primary_key=True)
    password = models.CharField(verbose_name='password',max_length=20)
    name = models.CharField(verbose_name='Employee name',max_length=20, blank=False, null=True)
    roll = models.CharField(verbose_name='Position',max_length=10, blank=False, null=True)
    nyushabi = models.DateField(verbose_name='Hire date',blank=False, null=True)
    taishabi = models.DateField(verbose_name='Leaving date',blank=True, null=True)
    hyoujijyun = models.IntegerField(verbose_name='Display order',unique=True)
    jikyu = models.IntegerField(verbose_name='Hourly wage',blank=False, null=True)
    delete = models.BooleanField(verbose_name='Delete flag',default=False)
    regist_date = models.DateTimeField(verbose_name='Registered Date', auto_now_add=True)
    regist_user = models.CharField(verbose_name='Registered user', max_length=50, null=True)
    update_date = models.DateTimeField(verbose_name='Update date and time', auto_now=True)
    update_user = models.CharField(verbose_name='Update user', max_length=50, null=True)

    def __str__(self):
        return self.name

Fix and migrate

image.png

This completes the fix. How many times will I recreate the table (laughs)?

Next, I would like to create a screen for registering users.

Recommended Posts

django makemigarations createsuperuser
Django
Django note 4
Django memorandum
Django installation
Django test
Django # 2 (template)
Django Note 5
Django hands-on
Touch django
django notes
Django Summary
Django basics
Django Shoho
Django defaults
Django + Docker
Django Glossary
Django search
Install Django
Django: References
Django Note 1
Django note 3
Django note 2
Django startup
Django notes
Django NullCharField