Djano Admin site学习 1:
一、admin注册app中的模型:
第一种方式:
from django.contrib import admin from myproject.myapp.models import Author admin.site.register(Author) 第二种方式(装饰器 @register):
from django.contrib import admin from .models import Author @admin.register(Author) class AuthorAdmin(admin.ModelAdmin): pass 二、admin中注册多模型: 首先在models.py中将模型创建好如:
class Person(models.Model): first_name = models.CharField(max_length=30) last_name=models.CharField(max_length=30)class Contact(models.Model): name = models.CharField(max_length=50) age = models.IntegerField(default=0) email = models.EmailField() def __str__(self): return self.nameclass Tag(models.Model): name = models.CharField(max_length=50) def __str__(self): return self.name 然后再在admin.py中修改第一项中的代码:
@admin.register(Contact)class ContactAdmin(admin.ModelAdmin): list_display = (‘name‘, ‘age‘, ‘email‘) search_fields = (‘name‘,) admin.site.register([Person, Tag]) 按上述代码完成的App——BLOG 主界面效果图如下:
原文地址:https://www.cnblogs.com/loiyxiaoj/p/10477848.html
时间: 2024-11-02 05:44:22