Django模型和管理面板

时间:2013-03-18 16:51:04

标签: django django-models django-admin django-orm

大家好我有这个型号

class Class1(models.Model):
    ....
    ctime = models.FloatField()  # I am storing date in timestamp here
    ....

是否可以在管理面板中显示ctime字段而不是浮点数,而不是时间字段?

1 个答案:

答案 0 :(得分:0)

检查Django documentation。您可以覆盖模型字段的默认小部件 -

from django.db import models
from django.contrib import admin

# Import our custom widget and our model from where they're defined
from myapp.widgets import RichTextEditorWidget
from myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.TextField: {'widget': RichTextEditorWidget},
    }

或者,对于单个模型,如this answer -

中所述
class StopAdminForm(forms.ModelForm):
  class Meta:
    model = Stop
    widgets = {
      'approve_ts': ApproveStopWidget(),
    }

class StopAdmin(admin.ModelAdmin):
  form = StopAdminForm

检查该问题中的其他答案.a