Setting up a foreign key to an abstract base class with Django

前端 未结 2 1836
青春惊慌失措
青春惊慌失措 2021-01-01 09:28

I\'ve factored out common attributes from two classes into an abstract base class, however I have another model that needs to reference either one of those classes. It\'s no

2条回答
  •  被撕碎了的回忆
    2021-01-01 10:09

    A generic relation seems to be the solution. But it will complicate things even further.

    It seems to me; your model structure is already more complex than necessary. I would simply merge all three Answer models into one. This way:

    • Answer_Risk would work without modification.
    • You can set resident to None (NULL) in case of an Answer_A.
    • You can return different string represantations depending on resident == None. (in other words; same functionality)

    One more thing; are your answers likely to have more than one risk? If they'll have none or one risk you should consider following alternative implementations:

    • Using a one-to-one relationship
    • Demoting risk as a field (or any number of fields) inside Answer class.

    My main concern is neither database structure nor performance (although these changes should improve performance) but code maintainability.

提交回复
热议问题