如何为Django相关对象集实现通用接口?

时间:2022-09-02 09:12:20

Here's the deal:

这是交易:

I got two db models, let's say ShoppingCart and Order. Following the DRY principle I'd like to extract some common props/methods into a shared interface ItemContainer.

我有两个db模型,比如ShoppingCart和Order。遵循DRY原则,我想将一些常见的道具/方法提取到共享接口ItemContainer中。

Everything went fine till I came across the _flush() method which mainly performs a delete on a related object set.

一切都很顺利,直到遇到主要在相关对象集上执行删除的_flush()方法。

class Order(models.Model, interface.ItemContainer):

# ...

def _flush(self):
    # ...
    self.orderitem_set.all().delete()   

So the question is: how do I dynamically know wheter it is orderitem_set or shoppingcartitem_set?

所以问题是:我如何动态地知道它是orderitem_set还是shoppingcartitem_set?

2 个解决方案

#1


First, here are two Django snippets that should be exactly what you're looking for:

首先,这里有两个Django片段,应该是您正在寻找的:

Second, you might want to re-think your design and switch to the django.contrib content types framework which has a simple .model_class() method. (The first snippet posted above also uses the content type framework).

其次,您可能需要重新考虑您的设计并切换到具有简单.model_class()方法的django.contrib内容类型框架。 (上面发布的第一个片段也使用内容类型框架)。

Third, you probably don't want to use multiple inheritance in your model class. It shouldn't be needed and I wouldn't be surprised if there were some obscure side affects. Just have interface.ItemContainer inherit from models.Model and then Order inherit from only interface.ItemContainer.

第三,您可能不希望在模型类中使用多重继承。它不应该被需要,如果有一些模糊的副作用,我也不会感到惊讶。只需让interface.ItemContainer继承自models.Model,然后继承自Interface.ItemContainer的Order。

#2


You can set the related_name argument of a ForeignKey, so if you want to make minimal changes to your design, you could just have ShoppingCartItem and OrderItem set the same related_name on their ForeignKeys to ShoppingCart and Order, respectively (something like "item_set"):

您可以设置ForeignKey的related_name参数,因此如果您想对设计进行最小的更改,您可以让ShoppingCartItem和OrderItem分别在其ForeignKeys上设置相同的related_name到ShoppingCart和Order(类似于“item_set”):

order = models.ForeignKey(Order, related_name='item_set')

and

cart = models.ForeignKey(ShoppingCart, related_name='item_set')

#1


First, here are two Django snippets that should be exactly what you're looking for:

首先,这里有两个Django片段,应该是您正在寻找的:

Second, you might want to re-think your design and switch to the django.contrib content types framework which has a simple .model_class() method. (The first snippet posted above also uses the content type framework).

其次,您可能需要重新考虑您的设计并切换到具有简单.model_class()方法的django.contrib内容类型框架。 (上面发布的第一个片段也使用内容类型框架)。

Third, you probably don't want to use multiple inheritance in your model class. It shouldn't be needed and I wouldn't be surprised if there were some obscure side affects. Just have interface.ItemContainer inherit from models.Model and then Order inherit from only interface.ItemContainer.

第三,您可能不希望在模型类中使用多重继承。它不应该被需要,如果有一些模糊的副作用,我也不会感到惊讶。只需让interface.ItemContainer继承自models.Model,然后继承自Interface.ItemContainer的Order。

#2


You can set the related_name argument of a ForeignKey, so if you want to make minimal changes to your design, you could just have ShoppingCartItem and OrderItem set the same related_name on their ForeignKeys to ShoppingCart and Order, respectively (something like "item_set"):

您可以设置ForeignKey的related_name参数,因此如果您想对设计进行最小的更改,您可以让ShoppingCartItem和OrderItem分别在其ForeignKeys上设置相同的related_name到ShoppingCart和Order(类似于“item_set”):

order = models.ForeignKey(Order, related_name='item_set')

and

cart = models.ForeignKey(ShoppingCart, related_name='item_set')