简体   繁体   English

如何组合两个嵌套元组并在 Django 的 CharField 选择中使用?

[英]How to combine two nested tuples and use in Django's CharField choices?

I have two nested tuples similar to the ones below which will be used in choices in Django's model.CharField() .我有两个类似于下面的嵌套元组,它们将用于 Django 的model.CharField() 的选择中。

COMPANIES = (
    ('USA', (
          ('gm', 'General Motors'), 
          ('tesla', 'Tesla'),
          ('ford', 'Ford')
        )
    ),
    ('South Korea', (
          ('kia', 'Kia Motors'),
          ('hyundai', 'Hyundai Motors'),
        )
    ),
    ('Japan', (
          ('nissan', 'Nissan Motors'),
          ('honda', 'Honda Motors'), 
          ('toyota', 'Toyota Motors'), 
        ) 
    ),
)

MANAGERS = (
    ('USA', (
          ('jack', 'Jack Smith'), 
          ('doyun', 'Doyun Kim'),
          ('jill', 'Jill Maggie'),
          ('akari', 'Akari Tanaka'),  
        )
    ),
    ('South Korea', (
          ('doyun', 'Doyun Kim'),
          ('siu', 'Siu Park'),
          ('jill', 'Jill Maggie'),  
        )
    ),
    ('Japan', (  
          ('akari', 'Akari Tanaka'), 
          ('jack', 'Jack Smith'), 
          ('haruto', 'Haruto Nakamura'), 
        ) 
    ),
)

Currently my working my working fields looks like this:目前我的工作领域是这样的:

companies = models.CharField(max_length=30, choices=COMPANIES)
managers = models.CharField(max_length=50, choices=MANAGERS)

But for the fidelity of the data, I don't want to repeat the country names twice and possibly have data like this (or something similar):但是为了数据的保真度,我不想重复两次国家名称,并且可能有这样的数据(或类似的东西):

COMPANY_INFO =  (
    ('USA', (
          ('gm', 'General Motors'), 
          ('tesla', 'Tesla'),
          ('ford', 'Ford')
        ), (
          ('jack', 'Jack Smith'), 
          ('doyun', 'Doyun Kim'),
          ('jill', 'Jill Maggie'),
          ('akari', 'Akari Tanaka'),  
        )       
    ),
    ('South Korea', (
          ('kia', 'Kia Motors'),
          ('hyundai', 'Hyundai Motors'),
        ), (
          ('doyun', 'Doyun Kim'),
          ('siu', 'Siu Park'),
          ('jill', 'Jill Maggie'),  
        )
    ),
    ('Japan', (
          ('nissan', 'Nissan Motors'),
          ('honda', 'Honda Motors'), 
          ('toyota', 'Toyota Motors'), 
        ), (
          ('akari', 'Akari Tanaka'), 
          ('jack', 'Jack Smith'), 
          ('haruto', 'Haruto Nakamura'), 
        ) 
    ),
)

Two questions:两个问题:

1) How can I write the model.Charfield() so that it will use COMPANY_INFO ? 1) 如何编写model.Charfield()以便它使用COMPANY_INFO

companies = models.CharField(max_length=30, choices=COMPANY_INFO[some magic here])
managers = models.CharField(max_length=50, choices=COMPANY_INFO[some other magic here])

COMPANY_INFO does not have to be a tuple as long as it is accepted by model.Charfield() ,ie following condition is enough: COMPANY_INFO不必是元组,只要它被model.Charfield()接受,即以下条件就足够了:

An iterable (eg, a list or tuple) consisting itself of iterables of exactly two items (eg [(A, B), (A, B) ...]) to use as choices for this field.一个可迭代对象(例如,列表或元组)由恰好两个项目(例如 [(A, B), (A, B) ...])的可迭代对象组成,用作该字段的选择。

2) (BONUS) How can I make lookups easily using COMPANY_INFO ? 2)(奖励)如何使用COMPANY_INFO轻松进行查找? Specifically, what is the Python code to get answers to questions like: "What are the company abbreviations in USA?", "What is the first and lastname of 'siu'?".具体来说,获得以下问题的答案的 Python 代码是什么:“美国的公司缩写是什么?”,“'siu' 的名字和姓氏是什么?”。

Yes you can specify this as:是的,您可以将其指定为:

COMPANIES = tuple((k, v) for (k, v, __) in COMPANY_INFO)
MANAGERS = tuple((k, v) for (k, __, v) in COMPANY_INFO)

So you can define this in your fields like:因此,您可以在您的字段中定义它,例如:

companies = models.CharField(
    max_length=30,
    choices=tuple((k, v) for (k, v, __) in COMPANY_INFO)
)
managers = models.CharField(
    max_length=50,
    choices=tuple((k, v) for (k, __, v) in COMPANY_INFO)
)

But based on your second question:但基于你的第二个问题:

2) (BONUS) How can I make lookups easily using COMPANY_INFO? 2) (BONUS) 如何使用 COMPANY_INFO 轻松进行查找? Specifically, what is the Python code to get answers to questions like: "What are the company abbreviations in USA?"具体来说,获得以下问题的答案的 Python 代码是什么:“美国的公司缩写是什么?”

I think it might make more sense to make separate models for Company and Manager .我认为为CompanyManager制作单独的模型可能更有意义。 Here your data is static, which can result in some problems if for example you want to add a company/mananger, rename one, or remove one.这里您的数据是静态的,这可能会导致一些问题,例如,如果您要添加公司/经理、重命名或删除一个公司/经理。

A ForeignKey to models, also easily allows you to query the database, make changes, and store extra data about the company/manager.一个ForeignKey到款,也很容易让你查询数据库,进行更改,并额外的数据存储有关公司/经理。

CharField s with choices are typically used if the choices are static : for example the states in the United States are probably quite static (well there have been plans some decades ago to make the Vatican, Great Britain, etc. United States states, but as far as I know, these plans never really took "momentum").如果选择是静态的,通常会使用带有选择的CharField s:例如,美国的州可能非常静态(几十年前就有计划将梵蒂冈、英国等作为美国的州,但作为据我所知,这些计划从未真正占据“势头”)。 The gender of a person is another one.一个人的性别是另一个。

But companies and managers have typically a more dynamic nature.但是公司和经理通常具有更动态的性质。 What if Doyun Kim no longer is a manager?如果 Doyun Kim 不再是经理怎么办? Or moves to Japan?还是移居日本?

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

相关问题 如何设置一个有选择的字符域 django 或使用哪个小部件? - How to style a charfield which has choices in it django or which widget to use? 如何在Django的FileField / CharField / BooleanField / Upload按钮上使用引导程序 - How to use bootstrap on Django's FileField/CharField/BooleanField/Upload button 如何根据model.django中的charfield选项过滤用户 - How to filter users depending on a models.charfield choices in django 如何合并两个元组列表以创建嵌套字典? - How to combine two lists of tuples to create a nested dictionary? 带选择的Django CharField,需要模型实例 - Django CharField w/Choices, model instance needed Django:限制模型中的选择。PrimaryKey 的字符域 - Django: Limit choices in a models.Charfield by PrimaryKey 带有选择的Django CharField,是否自动将可能的选择添加到syncdb上的数据库或迁移? - Django CharField with choices, auto add possible choices to database on syncdb or migrate? Django 1.8.2(使用Python 3.4):带有选择项的CharField如何存储在带有ENUM列的MySQL表中? - Django 1.8.2 (using Python 3.4): How is a CharField with choices stored in a MySQL table with an ENUM column? 如何在Django中搜索CharField? - How to search CharField in Django? Django queryset批注,具有选择项并尝试获取显示值的charfield - Django queryset annotation, charfield with choices and trying to get the display value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM