我在Django 1.8中有管理面板,我的代码在admin.py文件中:
以下是我模特的一部分:
TYPES_CHOICES = (
('normal', 'normal'),
('archive', 'archive'),
)
class Period(models.Model):
date_start = models.DateTimeField(help_text='date_start')
date_end = models.DateTimeField(help_text='date_end')
type = models.CharField(choices=TYPES_CHOICES, default=TYPES_CHOICES[0][0], max_length=10)
class PeriodAdmin(admin.ModelAdmin):
list_display = ('id', 'date_start', 'date_end', 'description',
'note', 'is_published', 'is_actual', 'type', )
search_fields = ('id', 'note', 'description', 'description_en',)
fields = ['type', ]
我的问题是,如何将类型字段切换到可编辑字段,切换字段值表单模型。我发现这不是文档。 请提出任何提示。
答案 0 :(得分:2)
我认为你想要列表上的可编辑字段
models.py :
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
TYPES_CHOICES = (
('normal', 'normal'),
('archive', 'archive'),
)
class Period(models.Model):
date_start = models.DateTimeField(help_text='date_start')
date_end = models.DateTimeField(help_text='date_end')
period_type = models.CharField(choices=TYPES_CHOICES, default=TYPES_CHOICES[0][0], max_length=10)
admins.py :
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from .models import Period
class PeriodAdmin(admin.ModelAdmin):
list_display = ('id', 'date_start', 'date_end', 'period_type',)
list_editable = ('date_start', 'date_end', 'period_type', )
admin.site.register(Period, PeriodAdmin)