Y
Y
yeputons2011-08-09 19:35:18
Django
yeputons, 2011-08-09 19:35:18

Django - what's the best way to bind multiple elements to a model (unknown how many)?

Good evening.
There is a certain model in Django. And I want to make it possible for the user to set several properties to the object, unique for each object.
For example: price: 300 rubles, goodness: 5+. Or: weight: 256 kg, volume: 0.5 liters. Those. the properties are unique, not related in any way (although it is planned to add a few “general” recommendations) and it is not known how many of them will be (but some reasonable amount).
The question arises - how best to do this so that there are as few problems as possible with working in jang, storing in the database, changing, deleting / adding.
So far, there are two options - concatenate through something and save as VARCHAR. But this is somehow dreary and it will be necessary to split it every time the model is loaded.
The second option is to make a MultiToMultiField and dynamically create/delete properties in a separate table. It seems like a good option, but there may be problems with deletion and modification. For example, when a field is deleted, nothing should happen, but when the model is deleted, all corresponding fields should be deleted. Maybe something simple can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fak3, 2011-08-09
@Fak3

And what prevents you from doing this: So when deleting a parameter, the products themselves will not be deleted, and when deleting a product, its parameters will be deleted
class Product(Model):
    ''' то, к чему привязываем параметры '''
    # тут поля модели
class ProductParam(BaseModel):
    name = models.CharField(max_length=255,verbose_name=u'Параметр')
    value = models.CharField(max_length=255,verbose_name=u'Значение')
    product = models.ForeignKey(Product,verbose_name=u'Товар')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question