Django-32-admin之list_display后台显⽰表字段前⾔
点击"⽤户表"进⼊如下页⾯,如想要在列表中展⽰我们想要看到的字段。
在django_study.app01.admin.py⽂件中注释掉之前的
# ister(User) # 注册user表
# ister(City) # 注册city表
# ister(Book) # 注册book表
# ister(Author) # 注册author表
# ister(AuthorDetails) # 注册author_details表
@ister(User)
class UserAdmin(admin.ModelAdmin):
list_display = ("name", "age", "sex", "city") # 列表显⽰字段
@ister(City)
class CityAdmin(admin.ModelAdmin):
list_display = ("city_id", "city_name")
@ister(Book)
class BookAdmin(admin.ModelAdmin):
list_display = ("name", "price", "pub_date")
@ister(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = ("name", "age", "details")
@ister(AuthorDetails)
class AuthorDetailsAdmin(admin.ModelAdmin):
list_display = ("hobby", "addr", "sch")
运⾏状态访问如图(字段名显⽰如下)
如果需要显⽰中⽂,在定义属性时加上verbose_name属性
django_dels.py(以⽤户表为例)
class User(models.Model):
"""
1.⼀个类默认⽣成⼀张表,默认的表名为:app名_类名
2.属性名就是表中的字段名
"""
name = models.CharField(max_length=64, verbose_name="姓名")
age = models.IntegerField(verbose_name="年龄")
sex = models.CharField(max_length=2, verbose_name="性别")
# 与city表关联,关联字段是city表id,可以为空,设置为级联删除
city = models.ForeignKey("City", to_field="city_id", null=True, on_delete=models.CASCADE, verbose_name="城市") # 定义外键
class Meta:
django admin 自定义页面"""重新定义表名为user"""
db_table = "user"
verbose_name_plural = "⽤户表"
运⾏状态访问
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论