Django 定制后台和修改模型 当数据模型建立完成时,接下来需要将模型里的数据展示出来,而且是通过模板这种类型进行显示 定制admin后台 日期类型 class Article(models.Model): created_time =
Django 定制后台和修改模型
当数据模型建立完成时,接下来需要将模型里的数据展示出来,而且是通过模板这种类型进行显示
定制admin后台
created_time = models.DateTimeField(auto_now_add=True)
last_updated_time = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User, on_delete=models.DO_NOTHING, default=1)
is_deleted = models.BooleanField(default=False)
readed_num = models.IntegerField(default=0)
from django.contrib.auth.models import User
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length=30)
content = models.TextField()
created_time = models.DateTimeField(auto_now_add=True)
last_updated_time = models.DateTimeField(auto_now=True)
author = models.ForeignKey(User, on_delete=models.DO_NOTHING, default=1)
is_deleted = models.BooleanField(default=False)
readed_num = models.IntegerField(default=0)
def __str__(self):
return "<Article: %s>" % self.title
同步数据
修改admin.py
from .models import Article
# Register your models here.
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ("id", "title", "content", "author", "is_deleted", "created_time", "last_updated_time")
ordering = ("-id",)
- 通过访问http://127.0.0.1:8000/admin