我有一个
Django项目
我正在开发一个电子商务应用程序。我在用
数据库
作为我的数据库,我正在尝试添加
creation_date
字段到我的
供应商产品
模型,以便记录产品创建的时间。
我做了什么
我添加了
创建日期
字段到我的
VendorProduct
模型如下:
models.py
(供应商产品型号)
from django.db import models
from django.utils.timezone import now
from userauths.models import CustomUser
class VendorProduct(models.Model):
user = models.ForeignKey(CustomUser, on_delete=models.SET_NULL, null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True, blank=True, null=True)
def __str__(self):
return self.title
移民
我运行了以下命令来应用迁移:
python manage.py makemigrations vendorpannel
python manage.py migrate vendorpannel
那么,我
已证实的
那个
创建日期
列存在于我的数据库中,使用:
from django.db import connection
with connection.cursor() as cursor:
cursor.execute("PRAGMA table_info(vendorpannel_vendorproduct);")
columns = cursor.fetchall()
for column in columns:
print(column)
输出
确认
那个
创建日期
存在于我的数据库中:
(7, 'creation_date', 'datetime', 0, None, 0)
管理面板配置
我更新了我的
管理面板
显示
创建日期
:
admin.py
from django.contrib import admin
from .models import VendorProduct
class VendorProductAdmin(admin.ModelAdmin):
list_display = ('creation_date')
readonly_fields = ('creation_date',)
admin.site.register(VendorProduct, VendorProductAdmin)
问题:
创建日期
未在Django管理中显示
即使在做出这些更改并应用迁移之后,
这个
创建日期
字段未出现在Django管理面板中
list_display
.
我尝试了什么
-
检查数据库中是否存在该字段
使用SQL查询(已确认)。
-
清除Django缓存并重新启动服务器
:
python manage.py collectstatic
python manage.py runserver
-
检查字段名称是否正确
在里面
list_display
(已确认)。
-
已验证
list_display
作品
对于其他字段(其他字段正确显示,仅
创建日期
缺失)。
预期成果
-
我希望看到
创建日期
显示在Django管理面板中的
供应商产品
.
-
该字段应为
只读
并且不应手动编辑。
提问
为什么是
创建日期
未出现
在Django管理面板中,即使:
-
它存在于数据库中。
-
已正确添加
list_display
.
-
迁移已成功应用。
我是什么
丢失的
在这里?