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

前端 未结 2 2016
没有蜡笔的小新
没有蜡笔的小新 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:34

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

    • Model inheritance with content type and inheritance-aware manager
    • ParentModel and ChildManager for Model Inheritance

    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).

    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.

提交回复
热议问题