问题
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 !!!
回答1:
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.
回答2:
Ok ... at the time of the writing, Django is just not good with that.
Schema-free databases such as mongodb are a good solution for this problem, and Django simply doesn't support those out of the box.
So I gave up Django, and rewrote the whole thing in node.js
with a mongodb database.
来源:https://stackoverflow.com/questions/8501822/coding-an-inventory-system-with-polymorphic-items-and-manageable-item-types