Django:如何使用枚举设置默认的model.charfield?

时间:2018-01-12 09:11:18

标签: django django-models enums

我在尝试使用模型中显示的默认值 Dim StartCell, EindCell As Range Dim teller As Integer Dim teller2 As Integer Cells(Application.ActiveCell.Row, 1).Select teller = 0 Do While Selection.Offset(teller, 0).Value = "" teller = teller - 1 Loop Selection.Offset(teller, 0).Select Set StartCell = ActiveCell teller2 = 0 Do While Selection.Offset(teller, 0).Value = "" teller = teller + 1 Loop Selection.Offset(teller, 0).Select Set EindCell = ActiveCell Range(StartCell, EindCell).Select Selection.Rows.Group Cells(Application.ActiveCell.Row, 8).Select ActiveCell.Formula = "=PRODUCT(RC[-4]:RC[-1])" Selection.NumberFormat = "0.00" Range(StartCell.Row + 1, 9).Formula = "=sum(cells(StartCell.row,8):Cells(EindCell.row,8))" 时出现问题。我收到以下错误。我按照here上写的内容,然而,看起来他们没有相同的问题。

错误消息

migrate

模型

field=models.CharField(choices=[('BIG','Larger'), ('MID','Normal'), ('SMALL','Small')], 
                       default=library.models.FontSize('Normal'), 
                       max_length=10),
AttributeError: module 'library.models' has no attribute 'FontSize'

choices.py

class Book(models.Model):

    class FontSize(ChoiceEnum):
        BIG = 'Larger'
        MID = 'Normal'
        SMALL = 'Small'

    #Other fields
    font_size = model.CharField(max_length=10, choices=FontSize.choices(), default=FontSize.MID)

1 个答案:

答案 0 :(得分:1)

替代实施,models.py

FONT_CHOICES = [
    ['BIG', 'Larger'],
    ['MID', 'Normal'],
    ['SMALL', 'Small'],
]

class FontSizeEnum(object):
    LARGER = 'BIG'
    NORMAL = 'MID'
    SMALL = 'SMALL'


class Book(models.Model)
    field = models.CharField(choices=FONT_CHOICES, 
                   default=FontSizeEnum.NORMAL, 
                   max_length=10)