ForeignKey or Choices: which to use?

Good day! Prompt, please, good people, it is better to use: choices or ForeignKey? Imagine a situation is, the object Item (thing) field unit (unit), what I need to do?
If I'm going to do with choices, then, for me, would be inconvenient to edit the name of existing units or add new ones.
If I do it with ForeignKey, I think, decreases productivity due to the extra queries to the database.
Please explain which of the two evils?

Code choices
#...
class Item(models.Model):
#...
 UNIT = (
 ("kg", "weight"),
 ("m", "meters"),
 ("mm", "millimeters")
)
#...
 unit = models.CharField(choices = UNIT)
 #...
Code with ForeignKey
#...
class Item(models.Model):
#...
 unit = models.ForeignKey("Unit")
 #...
March 19th 20 at 08:34
3 answers
March 19th 20 at 08:36
Solution
If you think that will quite often change or add units of measure, use ForeignKey. In terms of performance, add to the queries prefetch_related, a very good thing, + 1 request will not affect performance. But there too there are nuances that should be considered.
March 19th 20 at 08:38
Premature denormalization - anti-pattern.
Judging by your description of the problem, you need a ForeignKey, just greedily pull the related entity.
Tell me near, please, that means "just greedily pull the related entity"? - Sierra.Mil commented on March 19th 20 at 08:41
As I understand, choices is the optimization of the number of queries to the database (denormalization)? - Sierra.Mil commented on March 19th 20 at 08:44
@Sierra.Mil, prefetch_related - Blanche_Macejkovic commented on March 19th 20 at 08:47
There are greedy and lazy form download. If the lazy form of the related entities did not immediately catch up and this happens then in use, which adversely affects performance when using collections of these entities. - Blanche_Macejkovic commented on March 19th 20 at 08:50
March 19th 20 at 08:40
I personally do not see any reason to keep our database choices if they are constant, and the maximum is a year you will add it kilometers...
You must be wanted to say that it makes no sense to make a separate table for that, or what? Just because choices a flip side -- are stored the values in each line are repeated, then the more you eat places? - Sierra.Mil commented on March 19th 20 at 08:43
@Sierra.Mil,
1. If you wrote the system in which these "devouring space", then the original question would not have existed.
2. If you are so critical of those few kilobytes of "extra" you can always do
KG=0
G=1
CHOICES = (
 (KG, 'kg'),
 (G, 'g')
)
- ellsworth.Heaney commented on March 19th 20 at 08:46

Find more questions by tags Django