简体   繁体   中英

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() .

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 ?

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:

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.

2) (BONUS) How can I make lookups easily using 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'?".

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? Specifically, what is the Python code to get answers to questions like: "What are the company abbreviations in USA?"

I think it might make more sense to make separate models for Company and Manager . 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.

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"). 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? Or moves to Japan?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM