S
S
Stanislav Vladimirovich2021-01-07 21:32:45
PostgreSQL
Stanislav Vladimirovich, 2021-01-07 21:32:45

How productive is the product feature schema where feature values ​​are stored in two JSON fields (feature value and their structure?

For a very long time I was looking for an alternative to EAV for an online store on Django. The task was not only to implement a variable number of product attributes, but also to implement their ordering and nesting into attribute groups, namely: a
product can belong to several categories. The order of the categories is important because they define groups of attributes specific to each category. Within attribute groups, attributes are defined that can be of several types by data type - number, text, one value from the list, a set of values ​​from the list. Just one JSON field is a good option, but it describes an arbitrary number of attributes but does not specify their order and nesting. I had to farm two. The screenshots show what data structure is in them.
parameters = models.JSONField: The key is attribute_group id - attribute_id (an attribute can have a different value depending on which attribute group it belongs to).
5ff74b9a7a184799170935.png
parameters_structure = models.JSONField
5ff74bef7516d118956959.png
Written widget for admin:
class ProductAttributesField(forms.MultiValueField) which looks like this
5ff74cb967622806552207.png
.
Nesting is determined by tables and relationships between them:
Product, Category, ProductInCategory, AttrGroup, AttrGroupInCategory, Attribute, AttributesInGroup, AttributeValue (variants of fixed values ​​for selection)
These tables are needed in fact for the admin panel and store the names of these entities themselves. It looks like this:
In the form of a product (inline categories can be dragged to change the order of categories)
5ff74ef12b2d5017356884.png

list of categories:
5ff74f565a784393047965.png

in the category editing form:
5ff74f777c1eb589218331.png

Editing an attribute group:
5ff74fb7680cd985125505.png

Editing a product attribute:
5ff74ff97c63d492130429.png

Everything looks like EAV in the admin interface, but under the hood, any change in the data structure (added an attribute to an attribute group, changed the order of categories) overwrites the parameters_structure field of each related goods. All in order to be able to refer only to the product parameters when selecting and filtering products, and to parameters and parameters_structure when rendering product characteristics.
I have now implemented this logic in SAVE and DELETE intermediate tables (ProductInCategory, AttrGroupInCategory, AttributesInGroup) but in the process of transferring from there to the forms of changing the models themselves (Product, Category, AttrGroup)

There is still a lot of work, but is it justified? Will I get a speed gain compared to EAV, or maybe there was an easier solution?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question