Automatically write `admin.py` using `admin_generator` command of `django-extensions`

What i want to do:

If there are many tables, it will be difficult to write to admin.py.

I want you it to be done automatically.

If we use the admin_generator command of the library called django-extensions, it can be done automatically.

It really does his job regarding the contents of models.py.

What is django-extension?

django-extensions extends the functionality of manage.py, and there are various other commands.


$ pip install django-extensions

Add in to settings.py

  • pj_name/settings.py

    'django_extensions', # <- 追記


Overwrite the output result in admin.py.

$APP contains the application name created with $python manage.py startapp xxx.

$ python manage.py admin_generator $APP > $APP/admin.py


For example:

This class:

class Users(models.Model):
    user_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    first_name = models.CharField(max_length=255)
    email = models.CharField(max_length=255)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()
    del_flg = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'users'

Will be looking like this:

class UsersAdmin(admin.ModelAdmin):
    list_display = (
    list_filter = ('created_at', 'updated_at')
    date_hierarchy = 'created_at'

Create user to login on management screen

$ python manage.py createsuperuser

Username (leave blank to use 'your_home_dir'): <- any name as you wish
Email address: 
Password (again): 
Superuser created successfully.

Start the local server

$ python manage.py runserver

Access to this address:

It is OK if the login table is displayed with the user information created earlier.

All rights reserved

Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí