46

How to set default STATUSES option ?

class Order(models.Model):
    STATUSES = (
        (u'E', u'Expected'),
        (u'S', u'Sent'),
        (u'F', u'Finished'),
    )

    status = models.CharField(max_length=2, null=True, choices=STATUSES)
rockingskier
  • 9,066
  • 3
  • 40
  • 49
belisasun
  • 545
  • 1
  • 5
  • 9

2 Answers2

77
status = models.CharField(max_length=2, null=True, choices=STATUSES, default='E')

or to avoid setting an invalid default if STATUSES changes:

status = models.CharField(max_length=2, null=True, choices=STATUSES, default=STATUSES[0][0])
rockingskier
  • 9,066
  • 3
  • 40
  • 49
15

It's an old question, just wanted to add the now availabel Djanto 3.x+ syntax:

class StatusChoice(models.TextChoices):
    EXPECTED = u'E', 'Expected'
    SENT = u'S', 'Sent'
    FINISHED = u'F', 'Finished'

Then, you can use this syntax to set the default:

status = models.CharField(
    max_length=2,
    null=True,
    choices=StatusChoice.choices,
    default=StatusChoice.EXPECTED
)
Mehdi Zare
  • 1,221
  • 1
  • 16
  • 32