我正在使用Django Import导出通过管理页面导入书籍列表。我想知道是否可以自定义Django Admin页面,以便它为用户可以选择的月份字段提供一个模型选择字段
Models.py:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Book(models.Model):
JANUARY = '1'
FEBRUARY = '2'
MARCH = '3'
APRIL = '4'
MAY = '5'
JUNE = '6'
JULY = '7'
AUGUST = '8'
SEPTEMBER = '9'
OCTOBER = '10'
NOVEMBER = '11'
DECEMBER = '12'
MONTH_CHOICES = (
(JANUARY, 'January'),
(FEBRUARY, 'February'),
(MARCH, 'March'),
(APRIL, 'April'),
(MAY, 'May'),
(JUNE, 'June'),
(JULY, 'July'),
(AUGUST, 'August'),
(SEPTEMBER, 'September'),
(OCTOBER, 'October'),
(NOVEMBER, 'November'),
(DECEMBER, 'December'),
)
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, models.SET_NULL, blank=True, null=True)
author_email = models.EmailField('Author email', max_length=75, blank=True)
imported = models.BooleanField(default=False)
published = models.DateField('Published', blank=True, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, null=True, blank=True)
categories = models.ManyToManyField(Category, blank=True)
month = models.CharField(
max_length=2,
choices= MONTH_CHOICES,
default=JANUARY,
)
def __str__(self):
return self.name
admin.py
from django.contrib import admin
from import_export import resources
from .models import *
from import_export.admin import ImportExportModelAdmin
from import_export.fields import Field
from import_export.fields import *
from import_export.widgets import *
from django.dispatch import receiver
from import_export.signals import post_import, post_export
from import_export.admin import ImportExportActionModelAdmin
class BookResource(resources.ModelResource):
class Meta:
model = Book
import_id_fields = ('name','month',)
fields = ('name', 'price','month')
@admin.register(Book)
class BookAdmin(ImportExportActionModelAdmin):
resource_class = BookResource
admin.site.register(Author)
admin.site.register(Category)