开发者

Multi-Table Inheritance in Django. I'm not sure I understand

开发者 https://www.devze.com 2023-04-08 23:35 出处:网络
Im not sure I understand the advantage/purpose of multi-table inhe开发者_StackOverflow社区ritance… but it may be what I\'m looking for. Im dealing with Restaurants. My current logic is that I have a

Im not sure I understand the advantage/purpose of multi-table inhe开发者_StackOverflow社区ritance… but it may be what I'm looking for. Im dealing with Restaurants. My current logic is that I have a Company model which is likely (but not always) a Restaurant. Sometimes a Company can be a "parent" company, in-which case the Company model has a one-to-many with a Branch model. Both the Company and Branch models would have common fields, such as street address, contact info. If the Company has only one "branch" I can assume it's the Restaurant itself and so I don't need to attach a Branch object to the Company. Does this make sense? I know im repeating myself with with the street address [...] but it seems like an elegant way to store the data if I were to read the db directly.

Im not sure if multi-table inheritance is what I need. I just can't wrap my head around it by only looking at https://docs.djangoproject.com/en/dev/topics/db/models/#multi-table-inheritance.

edit: also open to taking any suggestions on a better db layout if im doing it wrong.


Model inheritance is useful in general because you do queries like Company.objects.all() to return all companies (including restaurants) and also Restaurant.objects.all() to return only restaurant companies. Just like 'regular' inheritance, it might be helpful to include common fields in a parent (Company) model on all the children models (Restaurant). For example, all Companies might have an address field, but only Restaurants might have a food_type field.

I've documented links to a few snippets that implement a "subclassing queryset" which basically lets you do a query like Company.objects.all() and have it return to you results like [<Company> < Restaurant>, <Company>, <Company>, < Restaurant> ]. Check out the link:

http://jazstudios.blogspot.com/2009/10/django-model-inheritance-with.html

The downside of this multi-table approach is that it introduces an extra join in your query between the Company parent table and the child Restaurant table.

An alternatieve would be to create an abstract model. This creates a separate table for Company and for Restaurant with redundant fileds. With multi-table inheritance, if we wanted to look up the address field on a Restaurant instance, we would be referencing (behind the scenes) the related Company model. With abstract inheritance, there would actually be an address field on the Restaurant table. Also, using the abstract inheritance, I don't think you can do Company.objects.all() and expect that it will return instances that were added as Restaurants, nor can you use the subclassing querysets from the snippits linked above.

Hope this helps, Joe

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号