How do I implement a common interface for Django related object sets?

前端 未结 2 2040
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 14:10

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 prop

2条回答
  •  我在风中等你
    2021-01-13 14:24

    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"):

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

    and

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

提交回复
热议问题