Mohammad AlMarzouq
Django Admin
Run the following management command in shell then follow instructions:
python manage.py createsuperuser
from django.contrib import admin
from .models import Post
admin.site.register(Post)
from django.contrib import admin
from .models import Post
class PostAdmin(admin.ModelAdmin):
pass
admin.site.register(Post, PostAdmin)
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
pass
class Post(models.Model):
STATUS = (
(0,"Draft"),
(1,"Publish")
)
title = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
body = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
updated_on = models.DateTimeField(auto_now=True)
status = models.IntegerField(choices=STATUS, default=0)
def __str__(self):
return self.title
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'status','created_on')
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'status','created_on')
list_filter = ("status",)
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'status','created_on')
list_filter = ("status",)
search_fields = ['title', 'content']
from django.contrib import admin
from .models import Post
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'status','created_on')
list_filter = ("status",)
search_fields = ['title', 'content']
prepopulated_fields = {'slug': ('title',)}
class Author(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
country = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(
'Author', on_delete=models.CASCADE)
title = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
from .models import Author, Book
class BookInline(admin.TabularInline):
model = Book
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
Why not register Book Admin?
from .models import Author, Book
class BookInline(admin.StackedInline):
model = Book
@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]