是否可以将不同的产品对象连接到一个合约?
我的问题:
我希望我的客户能够将许多不同的产品连接到一个合同,而列出附加到合同的所有产品的代码感觉很脏。
我的模型
class Contract(models.Model):
# The contract, a customer can have many contracts
customer = models.ForeignKey(Customer)
class Product(models.Model):
# A contract can have many different products
contract = models.ForeignKey(Contract)
start_fee = models.PositiveIntegerField()
# A customer may have ordered a Car, a Book and a HouseCleaning,
# 3 completly different Products
class Car(Product):
brand = models.CharField(max_length=32)
class Book(Product):
author = models.ForeignKey(Author)
class HouseCleaning(Product):
address = models.TextField()
要列出与合同相关的所有产品,代码如下所示:
c = Contract.objects.get(pk=1)
for product in c.product_set.all():
print product.book # Will raise an Exception if this product isnt a Book
我找不到任何合理的方法来找出产品是什么类型的产品!
我当前的解决方案
我已经这样解决了...但这整个混乱感觉...错误。我很高兴能得到任何正确方向的指示。
class Product(models.Model):
# (as above + this method)
def getit(self):
current_module = sys.modules[__name__]
classes = [ obj for name, obj in inspect.getmembers(current_module)
if inspect.isclass(obj) and Product in obj.__bases__ ]
for prodclass in classes:
classname = prodclass.__name__.lower()
if hasattr(self, classname):
return getattr(self, classname)
raise Exception("No subproduct attached to Product")
因此,我可以像这样获取每个特定产品(伪代码):
c = Contract.objects.get(pk=1)
for product in c.product_set.all():
print product.getit()
列出所有“真实”产品,而不仅仅是基本产品实例。
我需要什么帮助
建议以某种理智的方式做到这一点。
我不介意必须将所有内容抽象出一堆步骤,只是为了获得更清晰的代码。
My problem:
I want my Customers to be able to connect many different Products to one Contract, and the code to list all Products attached to a Contract feels dirty.
My Models
class Contract(models.Model):
# The contract, a customer can have many contracts
customer = models.ForeignKey(Customer)
class Product(models.Model):
# A contract can have many different products
contract = models.ForeignKey(Contract)
start_fee = models.PositiveIntegerField()
# A customer may have ordered a Car, a Book and a HouseCleaning,
# 3 completly different Products
class Car(Product):
brand = models.CharField(max_length=32)
class Book(Product):
author = models.ForeignKey(Author)
class HouseCleaning(Product):
address = models.TextField()
To list all the products connected to a Contract, the code looks something like this:
c = Contract.objects.get(pk=1)
for product in c.product_set.all():
print product.book # Will raise an Exception if this product isnt a Book
I can't find any sane way to find out what kind of product a Product is!
My current solution
I've solved it like this... but this whole mess feels... wrong. I'd be happy for any pointers in the right direction.
class Product(models.Model):
# (as above + this method)
def getit(self):
current_module = sys.modules[__name__]
classes = [ obj for name, obj in inspect.getmembers(current_module)
if inspect.isclass(obj) and Product in obj.__bases__ ]
for prodclass in classes:
classname = prodclass.__name__.lower()
if hasattr(self, classname):
return getattr(self, classname)
raise Exception("No subproduct attached to Product")
So i can fetch each specific product like this (pseudo-ish code) :
c = Contract.objects.get(pk=1)
for product in c.product_set.all():
print product.getit()
to list all the "real" products, and not just the baseproduct instance.
What I need help with
Sugggestions to do this in some kind of sane way.
I don't mind having to abstract everything away a bunch of steps, just to get cleaner code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个其他堆栈问题看起来直接相关——也许会有帮助?
具有集成查询集的子类化 django 模型
具体来说,该代码段具有子类化模型
餐食
沙拉(餐食)
http://djangosnippets.org/snippets /1034/
祝你好运!
This other stack question looks directly related -- maybe it will help?
Subclassed django models with integrated querysets
Specifically, the snippet has subclassed models
Meal
Salad(Meal)
http://djangosnippets.org/snippets/1034/
Good luck!
如果您知道客户永远不会订购
产品
,为什么不将其设为抽象模型,然后自己进行排序呢?此时,访问用户的书籍、汽车或房屋清洁就变得很容易,因为您只需使用c.book_set.all()
、c.car_set.all()
, 等等。如果您想要附加到合同的所有
Product
列表,您就必须自己进行排序 - 但编写一个sorted
包装器并不难如果您正在寻找什么,请将所有内容放入清单中。If you know that a customer's never going to order a
Product
, why not make it an abstract model and then just do the sorting yourself? Accessing a user's books, cars, or house cleanings becomes easy at that point, because you can just usec.book_set.all()
,c.car_set.all()
, and so on.You'd have to do the sorting yourself if you wanted, say, a list of all
Product
s attached to a contract - but it's not hard to write asorted
wrapper to put everything into a list for you if that's what you're looking for.