standard abstract base model classes?

4 views
Skip to first unread message

Eric Drechsel

unread,
Sep 1, 2008, 6:14:01 PM9/1/08
to django-hotclub
I have several content types which should share basic fields:
* title
* author
* pub date

If the all subclassed a basic model I could do generic queries like
"all things authored by X" etc. Should I write my own base model? It'd
be great if there was something already out there for this purpose.

P.S. I'm coming from Drupal so I still have "everything is a node"
ringing in my ears L

Dougn

unread,
Sep 2, 2008, 1:06:49 AM9/2/08
to django-hotclub
you are best off asking this in the django-users group which has a
much, much wider readership (by 2 orders of magnitude).

It sounds like you want the context-types app
(django.contrib.contexttypes) to do most of teh heavy lifting in an
abstract base class.
There are literally thousands of posts in django-users saying 'it'd be
great if there was something like this already out there', and all of
them different.
Some of the people who say this end up going off and creating their
own open source project around it, and a few have had those rolled
into the django core.

-Doug

James Tauber

unread,
Sep 2, 2008, 4:21:06 PM9/2/08
to django-...@googlegroups.com

On Sep 2, 2008, at 1:06 AM, Dougn wrote:
> you are best off asking this in the django-users group which has a
> much, much wider readership (by 2 orders of magnitude).

Although for the reasons you give below, one might have more success
getting standard base models into pinax than django core.

Plus helping Drupal users migrate to Django is a co-BDFL-endorsed goal
of Pinax ;)

Milan Andric

unread,
Sep 2, 2008, 5:26:02 PM9/2/08
to django-...@googlegroups.com

Eric,

In the past (before model inheritance/queryset refactor) you could use
the user foreign key to get the various objects related to user X. So
you would have:

class Novel(models.Model):
author = ForeignKey(User)
published = DateTimeField()

class Magazine(models.Model):
author = ForeignKey(User)
credits = TextField()

then to get the querysets into one list for your template you might do
something like:

u = User.objects.get(username='bill')
qs1 = Novel.objects.filter(author=u)
qs2 = Magazine.objects.filter(author=u)
mylist = list(qs1) + list(qs2)

Then you can pass mylist to your template and render it. This doesn't
do the cool stuff with model inheritance but it usually works.

I'd imagine if you write a patch and submit to either Pinax or the
appropriate django project, you will likely get positive feedback.
Base models makes a lot of sense but only recently (month or two ago)
has model inheritance hit trunk, and is yet to be in an official
release (any day now).

--
Milan

Milan Andric

unread,
Sep 2, 2008, 6:23:25 PM9/2/08
to django-...@googlegroups.com
On Tue, Sep 2, 2008 at 4:26 PM, Milan Andric <man...@gmail.com> wrote:
> Base models makes a lot of sense but only recently (month or two ago)
> has model inheritance hit trunk, and is yet to be in an official
> release (any day now).

Eric,

Just to clarify, actually qs-refactor hit trunk April 26 ... so it's
been more than a month or two! Time flies.

http://code.djangoproject.com/wiki/QuerysetRefactorBranch

--
Milan

Rock

unread,
Sep 5, 2008, 3:19:31 PM9/5/08
to django-hotclub

You have to be careful before deciding on an abstract base class. I
have used it with some success, but also ran in to difficulties in a
different case when I later decided that I wanted some other class to
access the abstract class with a ForeignKey relationship (which failed
since the abstract class, of course, has no table much less a primary
key.)

Database tables don't support duck typing. If you really want queries
encompassing all authors, then that base table probably needs to be
concrete.

Reply all
Reply to author
Forward
0 new messages