David Ramirez
David Ramirez

Reputation: 392

How to have django model utils Choices handle lowercase and uppercase

How can i use uppercase characters when adding a property with lowercase choices

this is the payload im using Clover is capitalized but i want to know how i can have capitalized and lowercase Clover work at the same time using model_utils Choices

           "name": "Test",
           "pos_name": "Clover",

class Tenant(TimeStampedModel):
    POS = Choices('square', 'clover', 'omnivore')


    pos_name = models.CharField(choices=POS, max_length=50, blank=True, default='')
    name = models.CharField(max_length=100)

Upvotes: 2

Views: 461

Answers (1)

ipaleka
ipaleka

Reputation: 3957

Maybe by overriding model's __setattr__?

def __setattr__(self, attr, value):
    if attr == "pos_name":
        value = value.lower()
    super().__setattr__(attr, value)

Upvotes: 1

Related Questions