参考文章:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/ (The Django admin site)
List_display
models.py
from django.db import models from django.utils.html import format_html class Author(models.Model): name = models.CharField(max_length=30) email = models.EmailField(blank=True) website = models.URLField(blank=True) def namecase(self): return self.name.upper() namecase.short_description = ‘name case‘ def __unicode__(self): return u‘%s‘ % (self.name) def colored_name(self): return format_html(‘<span style="color:red">{} {} </span><b>{}</b‘, self.website, self.name, self.email) colored_name.allow_tags = True def my_property(self): return self.name + ‘ ‘ + self.email my_property.short_description = ‘Full spell of the name‘ full_name = property(my_property)
admin.py
from django.contrib import admin from dragonball.models import Author class AuthorAdmin(admin.ModelAdmin): list_display = (‘full_name‘,‘upper_case_name‘,‘colored_name‘,‘namecase‘,‘__str__‘,‘email‘,‘website‘) def upper_case_name(self, obj): return ("%s --> %s" % (obj.name.upper(), obj.email)) upper_case_name.short_description = ‘name & email‘
前端显示:
InlineModelAdmin
能够在一个models页面编辑另一个models,比如,在author页面添加blog。
两个子方法:
TabularInline
StackedInline
admin.py
from django.contrib import admin from dragonball.models import Blog class BlogInline(admin.TabularInline): model = Blog class AuthorAdmin(admin.ModelAdmin): inlines = [ BlogInline, ]
前端显示:
时间: 2024-10-15 14:57:15