简体   繁体   English

如何在Django中合并/嵌套EnumFields?

[英]How can I combine/nest EnumFields in Django?

Django-EnumFields lets you combine Enum fields in Django (the clue was in the title). Django-EnumFields可让您在Django中合并Enum字段(线索在标题中)。

Can you combine nest these? 你能把这些嵌套吗?

Here's an example that plays off the docs : 这是一个处理文档的示例:

from django.db import models
from django_enumfield import enum

class BeerStyle(enum.Enum):
    LAGER = 0
    STOUT = 1
    WEISSBIER = 2

class SoftDrinkStyle(enum.Enum):
    COKE = 3
    LEMONADE = 4

class Drink(models.Model):
    style = enum.EnumField(????, default=BeerStyle.LAGER)

I don't know what would go in place of ???? 我不知道会代替什么???? , or if there is a better way to get this nested/combination to play out with Django. ,或者是否有更好的方法来使此嵌套/组合与Django一起使用。 I'm mainly asking as I want Enum behaviour, with the ability to probe different types, eg in a save method, check for User age if the Drink is or type Beer . 我主要是想查询枚举行为,因为它具有探测不同类型的能力,例如在保存方法中,检查是否为User年龄(如果Drink是)或键入Beer

Is this possible? 这可能吗? Having played with this for a bit I don't see how. 玩了一段时间我不知道如何。

Having looked over how Python Enums work, this looks like the best behaviour to mock up 'subclasses' 研究了Python枚举的工作原理后,这似乎是模拟“子类”的最佳行为

from django.db import models
from django_enumfield import enum

class DrinkStyle(enum.Enum):
    LAGER = (0, 'Beer')
    STOUT = (1, 'Beer')
    WEISSBIER = (2, 'Beer')
    COKE = (3, 'SoftDrink')
    LEMONADE = (4, 'SoftDrink')

    def __init__(self, id, drink_type):
        self.id = id    
        self.type = drink_type

    @property
    def type(self):
        return self.drink_type

class Drink(models.Model):
    style = enum.EnumField(DrinkStyle, default=DrinkStyle.LAGER)

Then use DrinkStyle.COKE.type to return the type. 然后使用DrinkStyle.COKE.type返回类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将2个Django查询合并为一个对象限制的对象 - How can I combine 2 django query into one with object limit 如何在 Marshmallow 中人为地嵌套模式? - How can I artificially nest schemas in Marshmallow? 我可以嵌套virtualenvs? - can I nest virtualenvs? 如何在Django REST Framework中正确嵌套序列化程序? - How do I properly nest serializers in Django REST Framework? 如何链接/或组合我的三个 Django 模型以更好地最大化关系和效率 - How can I link/or combine my three Django models to better maximize relationship and efficency 当开发服务器无法运行最新的浏览器时,如何进行将Selenium和Django结合在一起的测试? - How can I make tests that combine Selenium and Django when the development server can't run recent browsers? 如何在 Python2 中嵌套格式字符串? - How can I nest format string in Python2? 我可以将{%include%}与django 1.11中的自定义过滤器结合使用吗? - Can I combine {% include %} with custom filter in django 1.11? 如何将Django中的ListCreateAPIView和RetrieveUpdateDestroyAPIView合并到支持所有四个CRUD操作的单个视图中? - How can i combine both ListCreateAPIView and RetrieveUpdateDestroyAPIView in django into a single view that supports all four CRUD operations? 如何嵌套任意数量的Python文件上下文管理器? - How can I nest an arbitrary number of Python file context managers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM