Coding an inventory system, with polymorphic items and manageable item types

后端 未结 2 1273
旧巷少年郎
旧巷少年郎 2020-12-22 00:51

We currently have an inventory system for our employees. It contains laptops, phones, but also ergonomic chairs, fridges or software licenses ... So very different stuff tha

2条回答
  •  北荒
    北荒 (楼主)
    2020-12-22 01:09

    The Satchmo project is a django based e-commerce solution, which allows to manage different products and different options for a given products. Maybe you could find some inspiration in there.

    Another great tool for inheritance and polymorphism management is the Django model utils project, and the InheritanceManager it provides.

    I'm using in in production, to manage different kind of products (shirts, jackets, etc.), and it's really helpful.

    ** Edit **

    As requested, an example of a inheritance.

    class Product(models.Model):
        objects = InheritanceManager()
        client = models.ForeignKey('clients.Client')
        price = models.PositiveIntegerField()
    
    
    class Shirt(Product):
        color = …
    
    class Pants(Product):
        …
    
    products = Product.objects.all().select_subclasses()
    # products[0] can be a Shirt, and products[1] can be a Pants
    

    Note that I had to tweak a few things to make the code work with select_related, because of this issue.

提交回复
热议问题