Code

Opened 5 years ago

Closed 5 years ago

#10356 closed (fixed)

proxy models: subclass a model without creating a new table

Reported by: rfk Owned by: mtredinnick
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Following a discussion with Malcolm on django-users, I've implemented support for what's tentatively named "proxy models". These provide a way for model subclasses to modify the behaviour of a model without creating a new database table. The canonical example seems to be subclassing the User model - you might want to add additional methods/managers/etc, but you still want to pull data directly from the main users table. Under the attached patch this would look like the following:

    class MyUser(User)
        def extra_method(self):
            print "I am a new method"
        class Meta:
            proxy = True

MyUser is now simply a different view onto the main User table, rather than a full-blown subclass with a separate table.

The main body of the patch is to db/models/base.py, where the meta attributes of proxy models are automatically set up. Since it's now possible for a model to be using the same (table,primary key) pair as one of its base classes, I've added a small optimisation in db/models/sql/query.py that avoids a needless join in this case. There's also some tests and a first stab at some documentation.

This patch also integrates with (but doesn't require) ticket #3163, by marking proxy models as unmanaged.

The django-users discussion leading to this patch can be found here: http://groups.google.com/group/django-users/browse_thread/thread/fb06ff8e2a296f9c

Attachments (1)

proxy_models.diff (13.2 KB) - added by rfk 5 years ago.
patch implementing proxy models

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by rfk

patch implementing proxy models

comment:1 Changed 5 years ago by rfk

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from proxy models: subclassing models without create a new table to proxy models: subclass a model without creating a new table

whoops, fixed bad grammar in ticket summary

comment:2 Changed 5 years ago by Alex

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by mtredinnick

  • Owner changed from nobody to mtredinnick

comment:4 Changed 5 years ago by mtredinnick

  • Component changed from Uncategorized to Database layer (models, ORM)

comment:5 Changed 5 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from new to closed

(In [10083]) Fixed #10356 -- Added pure-Python inheritance for models (a.k.a proxy models).

Large portions of this are needed for #5420, so I implemented it fully.
Thanks to Ryan Kelly for an initial patch to get this started.

Refs #5420.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.