S
S
Stanislav Fateev2015-07-01 10:14:27
Django
Stanislav Fateev, 2015-07-01 10:14:27

Objects with dynamically created properties in Django?

It is necessary to implement the ability for users to add additional properties to existing objects (length, width, weight, interface, color - whatever) What these properties will be, how many of them there will be in total and what they will contain as values ​​is not known. I think to implement three models, something like this:

class Item(models.Model):
    name = models.CharField(max_length=100)

class Property(models.Model):
    name = models.CharField(max_length=100)

class ItemProperties(models.Model):
    item = models.ForeignKey(Item)
    property = models.ForeignKey(Property)
    value = models.CharField(max_length=100)

Is this normal? Or are there more convenient (ready-made) solutions? I don’t really understand how to work with value then - if it can contain a wide variety of data - numbers, strings, geo-coordinates - can another PropertyType be introduced to determine the type of property?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
U
un1t, 2015-07-01
@svfat

Your idea is already released https://github.com/mvpdev/django-eav
Another idea is to use Elasicsearch or Sphinx fulltext engines for facets.

M
m0ody, 2015-07-09
@m0ody

In addition to the proposed HStoreField, there is also a JSONField. This is in case value can be not only a string, but also a number, bool, None. This field is currently only in the master branch ( https://github.com/django/django/blob/master/djang... ). Expected in django 1.9.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question