我寻找解决方案,但我相信我做错了什么,或者在 Django 4 中是不同的。
created_at
和 updated_at
字段不会出现在管理中。我不知道我在跳过什么。
家长班
class TimeStampMixin(models.Model):
class Meta:
abstract = True
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self) -> str:
return f'{self.created_at} - {self.updated_at}'
继承自TimeStampMixin的类
class Post(TimeStampMixin):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=200)
body = models.TextField()
category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True)
# tags = models.ManyToManyField('Tag')
tags = TaggableManager()
objects = models.Manager() # The default manager.
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='blog_posts')
publish = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
published = PublishedManager() # Our custom manager.
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail',
args=[self.publish.year,
self.publish.month,
self.publish.day, self.slug])
admin.py
文件中的管理规范
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'slug', 'author', 'publish', 'status', 'created_at', 'updated_at')
list_filter = ('status', 'created_at', 'publish', 'author')
search_fields = ('title', 'body')
prepopulated_fields = {'slug': ('title',)}
raw_id_fields = ('author',)
date_hierarchy = 'publish'
ordering = ('status', 'publish')
结果
问题是你的
created_at
和 updated_at
应该是 readonly_fields
。尝试以下方法
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
list_display = ('created_at', 'updated_at')
# your code
readonly_fields = ('created_at', 'updated_at')
会起作用的
这是您应该做的,然后应用迁移,然后再将父模型添加到
admin.py
。
class TimeStampMixin(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
abstract = True
def __str__(self):
return f'{self.created_at} - {self.updated_at}'
更好的是,可以将日期时间字段添加到您的子模型中,因为您没有将父级添加到管理员中,我想它应该可以工作。所以你可以做这样的事情。这只是一个替代示例..
class Post(TimeStampMixin):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=200)
body = models.TextField()
category = models.ForeignKey('Category', on_delete=models.SET_NULL, null=True)
# tags = models.ManyToManyField('Tag')
tags = TaggableManager()
objects = models.Manager() # The default manager.
slug = models.SlugField(max_length=250,
unique_for_date='publish')
author = models.ForeignKey(User,
on_delete=models.CASCADE,
related_name='blog_posts')
publish = models.DateTimeField(default=timezone.now)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
published = PublishedManager() # Our custom manager.