在forms.py内的Django选择字段

时间:2019-01-31 21:33:53

标签: django django-forms

我有一个模特

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

我正在尝试在查询集中创建一个月的字段。我该怎么做,我尝试了Book.objects.all()。values('month),但这只会给我现有数据,而不是所有月份

0 个答案:

没有答案