django admin.py code example

Example 1: django admin register mdoel

from django.contrib import admin
from myproject.myapp.models import Author

admin.site.register(Author)

Example 2: django runserver

python manage.py runserver

Example 3: create super user in django

python3 manage.py createsuperuser

Example 4: django admin register

from django.contrib import admin
from .models import Author

@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
    pass

Example 5: show post id on django admin interface

class BookAdmin(admin.ModelAdmin):
    readonly_fields = ('id',)

admin.site.register(Book, BookAdmin)

Example 6: what is admin.tabularinline django

from django.db import models

class Author(models.Model):
   name = models.CharField(max_length=100)

class Book(models.Model):
   author = models.ForeignKey(Author, on_delete=models.CASCADE)
   title = models.CharField(max_length=100)