Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

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

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 that admins can create/read/ update/delete.

After doing a version completely based on admin interface, I gave up cause it didn't provide enough flexibility. So I rolled-up a complete custom version, but there is far too much code to my taste ... it's a pain to maintain.

Some of the problems I have been facing include :

  • allowing the admins to add their own item types through an interface, e.g. : laptop, TV, ... so basically like if they could create Django models themselves with a set of attributes through an interface. Also item types are hierarchical, e.g. TV and Laptop are subclasses of ElectronicItem, which in turn is a subclass of Item, ...

  • polymorphism : when listing all the items, they should be aware of what type they are, this in order to search/filter the list with javascript and also generate urls to the item detailed view.

  • updating some attributes through Ajax, e.g. laptops have licenses. On a laptop detail page, I have a javascript "manager", to associate/detach licenses to that laptop.

So I was wondering if anybody had a suggestion on what to use ! I especially wonder if one of the django CMSes apps could help me, because that does sound like functionalities a CMS could provide ! I was even thinking of a NOSql database ... but those sound like complicated solutions.

It is actually not the first time I am facing this problem of polymorphism with Django, and I still haven't found a good solution. So I kind of hope, their is something that I have completely missed, and that somebody can show me the light !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...