2

I have been trying to figure out the best way (or correct) way to set up models for our PIM/PriceModel app in Django.

Example models (stripped):

class ProductItem(models.Model):
    """
    An actual item/product, what it all revolves around.
    """
    part_number = models.CharField(unique=True, max_length=50, help_text='')
    internal_part_number = models.CharField(primary_key=True, max_length=50, help_text='')  # prefilled by partnumber

    type = models.ForeignKey('Type', null=True, on_delete=models.SET_NULL)
    attributes = JSONField() # Or another dynamic field
    # and more ....


class Type(models.Model):
    """
    Product type i.e. camera-dome, camera-bullet, pir, etc.
    """
    pass


class Segment(models.Model):
    """
    A segment of the company like Industry, Retail, Guarding etc.
    """
    pass


class ProductCategory(models.Model):
    """
    Supposedly a child category of TopCategory.
    """
    pass


class InstallPrice(models.Model):
    """
    Product item installation prices based on Type, Product Category and Segment.
    """
    install_price = models.DecimalField(max_digits=8, decimal_places=2, help_text='')
    type = models.ForeignKey('Type', null=True, on_delete=models.SET_NULL)
    product_category = models.ForeignKey('ProductCategory', null=True, on_delete=models.SET_NULL)
    segment = models.ForeignKey('Segment', null=True, on_delete=models.SET_NULL)

Take a look at "attributes = HStoreField(db_index=True)" in the ProductItem model.

The main thing i need to store in a product item is attributes like how many inputs/outputs/connection-options does it have. I need to store this for testing products against each other further down the line in the price-model app. This to make sure you have the right amount of products with matching attributes like inputs or outputs. I also need the User/Admin to be able to add this attributes dynamically so the app becomes self sustainable and not requires a migration id there is a new attribute I dont yet know about.

As I could not figure out a reasonable model configuration I ended up looking at postgres specific fields. This is not a must!

ideally when selecting type in the admin section i would like a "preset" of attributes to be presented based on the type.

Attributes could be:

inputs  # number
outputs  # number
channels  # number
analysis  # Boolean 

Is this achievable? Any suggestions are welcome as I have limited Data Base experience. I need help figuring out the models.

Izzno
  • 81
  • 5
  • Is there a specific reason for using `HStoreField` and not `JsonField`? – Saad Aleem Dec 18 '18 at 18:51
  • Also, why have you kept the category in the price model rather than the Product model? – Saad Aleem Dec 18 '18 at 18:53
  • Nope, I would probably use (or think I am going to use JsonField). I just wrote the question before experimenting. Category is in both (it was not relevant for the question, so i snippet quite a bit of the models (or so i thought). – Izzno Dec 19 '18 at 15:46
  • Check out this link: https://stackoverflow.com/questions/37535618/jsoneditor-integration-with-django-admin It is about creating a json editor widget for JSONField in admin. –  Dec 19 '18 at 23:52
  • I don't know if one of these might work: https://github.com/abogushov/django-admin-json-editor https://github.com/nnseva/django-jsoneditor since you're using an `HStoreField`. Though it might be possible to extend the form fields those libraries provide to handle `HStoreField` since it returns a python dict. – tchatow Dec 20 '18 at 09:46

0 Answers0