adash
adash

Reputation: 29

Inheritance in Django model vs template usage

I am quite new to Django framework. I have a question(problem) to solve.

I have model design: Class "Pet" is abstract class. Three classes inherit from Pet: - Dog - Cat - Bird

Also there is a class Registration that has foreign key linked to Pet that can be any of those three kind of Pets. Could you help me what is the best practice to design a database structure like this and how to deal with it using Django template framework?

Upvotes: 1

Views: 120

Answers (1)

BATMAN
BATMAN

Reputation: 373

You want to implement something called Polymorphic Associations.

Maybe

  1. you should take a look at django-polymorphic, or

  2. If you are comfortable with making some changes in the Registration Model. Just add a column denoting what key is stored in which table, so you will have a column with Dog, Cat or Bird and another column with their id (Not as a Foreign Key but as a simple integer). In this way, once you create efficient functions for storing and fetching values, you won't really have to look at it again. (It worked for me)

Upvotes: 1

Related Questions