M
M
mugiwara_l2018-05-01 10:07:24
Django
mugiwara_l, 2018-05-01 10:07:24

How to make multiple values ​​for one field in Django?

Good afternoon!
I am a beginner, so do not be surprised if you see obvious blunders in what is written.
I make a product model in an online store. A product has characteristics, such as color or size, where there can be multiple values. I have 2 thoughts on how to do this:
1. Add to the product model and fill in as needed:
color1 = models.CharField(max_length=100, choices=ALL_COLORS, blank=True)
color2 = models.CharField(max_length=100, choices =ALL_COLORS, blank=True)
2. Make
colors = models.ManyToManyField(Color, verbose_name='colors')
And make the "Color" model in addition to make it work.
But the above is just rookie speculation. The question is how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2018-05-01
@mugiwara_l

Both methods
smell 1. Naming smells - well, you can normally name color_bottom, color_top
2. It smells like overengineering If
the products are homogeneous and they really only have color1 and color2, then it’s better

S
Sergey Gornostaev, 2018-05-01
@sergey-gornostaev

If PostgreSQL is used, then using JSONField is a good fit . If another DBMS, then you will have to use EAV .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question