代码之家  ›  专栏  ›  技术社区  ›  whitebear

不用数据库Django制作复杂模型

  •  0
  • whitebear  · 技术社区  · 3 年前

    在…上 django 3

    当我需要没有真实数据库(比如mysql)的数据时,我可以使用 m.IntegerChoices

    CallType 从未如此改变 IntegerChoices 这很合适。

    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)
    

    现在我想扩展 呼叫类型 更复杂。

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

    我应该使用什么练习?

    我害怕 整数选择 不适合这种情况。

    1 回复  |  直到 3 年前
        1
  •  1
  •   Iain Shelvington    3 年前

    将标签添加到 Django enumeration types ,将元组传递给每个成员,其中第一个元素是值,第二个元素是自定义标签

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