0

For a school project, I want to create a Django app similar to Woocommerce. What I mean by that is I want the site owner/admin to be able to add his own product attributes. Instead of creating classes for each type of product, I just wanted a simple Product class which would have attributes common to all products such as price and sku and a way for the admin to add custom attributes. I'll give you an example: let's say you just sell T-shirts on your e-commerce shop. It's easy, you create a field "size" with choices "S", "M" or "L" and another field "color" with different color choices. Now, imagine you create an e-commerce Django app which is supposed to work for different kinds of vendors. One will maybe sell cars, another house furniture, who knows? You want the store vendor admins to be able to create their own product attributes. Now you can't create a model with all possible fields like color, manufacturer, horsepower, height, ... because you would have too many fields for one model and you would certainly forget some.

I thought I could create 3 classes: Product, ProductAttribute, ProductAttributeValue and play with Foreign and ManyToMany fields. I was able to reproduce more or less the Woocommerce design but this isn't really elegant, and it made it difficult to display product filters in the templates using the attributes of products displayed on the page.

I thought another solution could be dynamic model fields, but I don't see how I could use these for my case here. I'm not sure how this works. If anyone thinks this is a way to go, please give me an example, I'd be happy to try.

Of course, the solution where you would create an abstract Product class and create child classes for your different types of products is dismissed because I want a more flexible system where I don't have to define product attributes in advance.

I'm wondering if this is even feasible in Django, based on how it was designed. Has anyone ever tried to create an e-commerce CMS in Django in the style of Woocommerce?

Thomas
  • 422
  • 1
  • 4
  • 12
  • 1
    I don't know about dynamic model fields, so I'm not sure about those; but I think this desire to give the user control over the codebase itself is faulty. If you want dynamic content, give your models attributes that have dictionaries as values, and create internal logic (in the model's methods and/or your views) to assign key-value pairs to those dictionaries as needed. This allows dynamic content to be stored inside your 'base' model, but does not allow user interaction with the model to literally modify the codebase itself, which is what you seem to want (in my opinion, unwisely). – m.arthur Sep 19 '20 at 18:20
  • @m.arthur What do you think of Woocommerce then? Is it ok that they allow admins to create their own attributes because in Woocommerce they are stored more like a value in a table than a class or class attribute? – Thomas Sep 19 '20 at 19:51

0 Answers0