Home > Enterprise >  Making complex model without database Django
Making complex model without database Django

Time:03-05

On django 3

When I need data without real database(like mysql), I can use m.IntegerChoices

This CallType never changes so IntegerChoices is suitable.

from django.db import models as m
class CallType(m.IntegerChoices):
    PULL = 1 
    PUSH = 2

class BaseCall(models.Model):
    class Meta:
        db_table = 't_BaseCall'

    call_type = m.PositiveSmallIntegerField(
        choices=CallType.choices, default=CallType.PULL)

Now I want to expand CallType more complex.

class CallType(m.IntegerChoices):
    PULL = {"number":1,"name":"pulling"}
    PUSH = {"number":2,"name":"pushing"}

What practice should I use?

I am afraid IntegerChoices is not suitable for this case.

CodePudding user response:

To add labels to Django enumeration types, pass tuples to each member where the first element is the value and second element is the custom label

class CallType(m.IntegerChoices):
    PULL = 1, "pulling"
    PUSH = 2, "pushing"
  • Related