0

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.

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you solving my problem for now. however I have another problem When I need more than second element. stackoverflow.com/questions/71359124/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.