views:

37

answers:

1

If I'm building an application that will have over 30 models, and I want the option to plug in a custom manager or other functionality into all the models down the road, is it a good idea to use an abstract base model and subclass it with every model, or is there compelling reasons not to do this?

+1  A: 

I started doing Django before the Great ORM Rewrite, so I got in the habit of creating a MixIn class that gave a lot of common functionality across all of our content classes. Stuff like common URL creation schemes, membership-level checking functions, etc. etc.

Since we used a very consistent naming scheme across all of our models, this approximates an ABC, but it's just Good Old Python. A model declaration would look like:

class SomeModel(models.Model, MixInClass):
    pass
Peter Rowell
@Peter Thanks. Do you think it's better to still do that with the new ORM or would you use ABC if you started from scratch now?
orokusaki
@orokusaki: It's a good question, but I don't have a general answer for it. At the time I was limited to adding methods and non-DB attributes so I wasn't doing anything that ABCs would have enhanced. The one recent project where I used model inheritance (not ABC) was a lesson in the limitations of the ORM in that area. As good as Django is, this is a tricky area and is highly susceptible to app-specific oddities.
Peter Rowell
@Peter 1) Thanks again, and 2) I agree strongly.
orokusaki