0

I am trying to write models for an inventory-type project in Django and am unsure how to best define my models. Do I choose Abstract Base Classes, Multi-Table Inheritance, Proxy Models or something else to get my models to behave according to these requirements:

  • A generic Product model that can track the stock level of a product
  • Admin users must be able to add a new generic Product via the admin
  • Specific Product models that can have the hardware serial number added as an attribute
  • Specific Product can be associated with my Order/Customer models

Is it possible to just do this and have it meet my reqs without any issues or is there a better approach?

class Product(models.Model):
    name = models.CharField(max_length=250)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    stock_level = models.IntegerField()

class SpecificProduct(Product):
    product = models.OneToOneField(Product)
    order = models.OneToOneField(Order)
    owner = models.OneToOneField(Customer)
    serial_nr = models.CharField(blank=True, max_length=250)

Ideally, I would like to have behavior that works something like this:

customer = Customer.objects.create([. . .])
order = Order.objects.create([. . .])
widget = Product.objects.create([. . .])
my_widget = SpecificProduct.objects.create(
    product=widget,
    order=order,
    owner=customer,
    serial_nr="12345"
)

Am I on the right track? Any help/advice would be much appreciated!

1 Answers1

0

Neither AbstractModel nor ProxyModel would be helpful because neither of these create table for you data (no abstract product table and no proxy special product table).

I don't really get why you need OneToOneField to your Product model as you inherit all of Product fields in SpecificProduct but if you need reference to base product instance that's a way to go. Consider django-mptt for your tree-like models. It provides a lot of useful tools for tree like structures.

scruffy_
  • 41
  • 4