extending admin interface

3 views
Skip to first unread message

Igor Goryachieff

unread,
Aug 30, 2005, 2:27:54 AM8/30/05
to django...@googlegroups.com
Hello, guys!

Is there a good solution to extend admin interface (including
additional fields for users table) without hacking Django sources?
In example I want to add extra fields for users table such as site,
birthdate, jabber and so on and manage it from admin interface.

Thank you!


--
Igor Goryachieff
Jabber: ig...@xmpp.ru
http://goryachev.org/

asrenzo

unread,
Aug 30, 2005, 5:21:23 AM8/30/05
to Django users
I guess this could help you :

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

Regards,

Laurent.

Igor Goryachieff

unread,
Aug 30, 2005, 5:55:49 AM8/30/05
to django...@googlegroups.com
On Tue, Aug 30, 2005 at 02:21:23AM -0700, asrenzo wrote:
> I guess this could help you :
>
> http://code.djangoproject.com/wiki/ModelInheritance

It seems to helpful for my purposes. Thanks!

Igor Goryachieff

unread,
Aug 30, 2005, 10:46:55 AM8/30/05
to django...@googlegroups.com
On Tue, Aug 30, 2005 at 02:21:23AM -0700, asrenzo wrote:

> I guess this could help you :
>
> http://code.djangoproject.com/wiki/ModelInheritance

This code doesn't work. Is it connected to ticket #122?

Adrian Holovaty

unread,
Aug 30, 2005, 10:56:42 AM8/30/05
to django...@googlegroups.com, Igor Goryachieff
On 8/30/05, Igor Goryachieff <ig...@jahber.org> wrote:
> > http://code.djangoproject.com/wiki/ModelInheritance
>
> This code doesn't work. Is it connected to ticket #122?

Yeah, whoever created that wiki page did it before the model syntax
changed. Here's an official example of subclassing models:

http://www.djangoproject.com/documentation/models/subclassing/

To replace a model that's already been defined, subclass the model to
add/remove fields, and use "replaces_module" META option, like so:

replaces_module = 'auth.users'

This particular feature is undocumented at this point because it's
quite advanced.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Adam

unread,
Aug 30, 2005, 11:56:41 AM8/30/05
to Django users
Yeah, I didn't update the wiki page because you told me the page you
pointed to was coming. I'll replace the previous wiki page with a link
to http://www.djangoproject.com/d ocumentation/models/subclassin g/

Adam

Igor Goryachieff

unread,
Aug 30, 2005, 5:22:52 PM8/30/05
to django...@googlegroups.com
On Tue, Aug 30, 2005 at 09:56:42AM -0500, Adrian Holovaty wrote:
> Yeah, whoever created that wiki page did it before the model syntax
> changed. Here's an official example of subclassing models:
>
> http://www.djangoproject.com/documentation/models/subclassing/

I have seen that already, but it did not helped me. :(

> To replace a model that's already been defined, subclass the model to
> add/remove fields, and use "replaces_module" META option, like so:
>
> replaces_module = 'auth.users'
>
> This particular feature is undocumented at this point because it's
> quite advanced.

Is this idea so non-trivial?

I make app for my project, then place into myproject/apps/myapp/models/myapp.py
the following:

from django.core import meta, validators
from django.models import core, auth

class User(auth.User):
jabber = meta.CharField(maxlength=35, blank=True)
class META:
replaces_module = 'auth.users'
admin = meta.Admin(
('Extra info', {'fields': ('jabber', )}),
)

And getting the traceback like:

...
File "/usr/lib/python2.3/site-packages/django-1.0.0-py2.3.egg/django/core/management.py", line 51, in get_sql_create
for klass in mod._MODELS:
AttributeError: 'module' object has no attribute '_MODELS'

What is the mistake here? And what should be specified as
module_name variable?

Thank you.

Adam

unread,
Aug 30, 2005, 7:06:12 PM8/30/05
to Django users
I haven't seen that error, but one thing I noticed about your model is
that I don't think you can just add things to the admin interface (like
you're trying to do with 'Extra info'). You have to copy the admin
section of auth.User if you want everything that's already there to
also show up. For instance, here's my User model:

class User(auth.User):
company = meta.ForeignKey(Company)
phone = meta.PhoneNumberField(blank=True)
extension = meta.SmallIntegerField(blank=True, null=True)
position = meta.CharField(maxlength=255, blank=True)


class META:
replaces_module = 'auth.users'
admin = meta.Admin(

fields = (
(None, {'fields': ('username', 'password_md5')}),
('Personal info', {'fields': ('first_name',
'last_name', 'email')}),
('Extra info', {'fields': ('company', 'phone',
'extension', 'position')}),
## ('Permissions', {'fields': ('is_staff', 'is_active',
'is_superuser', 'user_permissions')}),
('Important dates', {'fields': ('last_login',
'date_joined')}),
('Groups', {'fields': ('groups',)}),
),
list_display = ('username', 'email', 'first_name',
'last_name', 'is_staff'),
list_filter = ('is_staff', 'is_superuser'),
search_fields = ('username', 'first_name', 'last_name',
'email'),
)

Adrian Holovaty

unread,
Aug 30, 2005, 9:13:34 PM8/30/05
to django...@googlegroups.com
On 8/30/05, Adam <leftw...@gmail.com> wrote:
> I haven't seen that error, but one thing I noticed about your model is
> that I don't think you can just add things to the admin interface (like
> you're trying to do with 'Extra info'). You have to copy the admin
> section of auth.User if you want everything that's already there to
> also show up.

Yeah, Adam's right -- you need to copy the entirety of the admin.fields.

Regarding the error, you'll need to make sure your __init__.py file
contains "myapp" in its "__all__" variable, assuming "myapp.py" is the
name of your model module. See
http://www.djangoproject.com/documentation/model_api/#using-models for
full documentation (just added a couple of days ago!).

Igor Goryachieff

unread,
Aug 31, 2005, 12:19:13 PM8/31/05
to django...@googlegroups.com
On Tue, Aug 30, 2005 at 08:13:34PM -0500, Adrian Holovaty wrote:

> Regarding the error, you'll need to make sure your __init__.py file
> contains "myapp" in its "__all__" variable, assuming "myapp.py" is the
> name of your model module. See
> http://www.djangoproject.com/documentation/model_api/#using-models for
> full documentation (just added a couple of days ago!).

The problem still exists. Any ideas?

scottpierce

unread,
Sep 2, 2005, 10:38:20 PM9/2/05
to Django users
Post the code after making the recommended changes.

Igor Goryachieff

unread,
Sep 3, 2005, 8:04:07 AM9/3/05
to django...@googlegroups.com
On Tue, Aug 30, 2005 at 08:13:34PM -0500, Adrian Holovaty wrote:
> Regarding the error, you'll need to make sure your __init__.py file
> contains "myapp" in its "__all__" variable, assuming "myapp.py" is the
> name of your model module. See

The issue was resolved after making one change in file
django/core/meta/__init__.py:

Index: django/core/meta/__init__.py
===================================================================
--- django/core/meta/__init__.py (revision 618)
+++ django/core/meta/__init__.py (working copy)
@@ -673,7 +673,7 @@
# contain this list:
# [<class 'django.models.polls.Poll'>, <class 'django.models.polls.Choice'>]
# Don't do this if replaces_module is set.
- app_package.__dict__.setdefault('_MODELS', []).append(new_class)
+ app_package.__dict__.setdefault('_MODELS', []).append(new_class)

# Cache the app label.
opts.app_label = app_label

Extending admin interface now works, but it's configuration process is a bit
tricky, as you mentioned before. After this change I had to execute
the following to update the database:

% django-admin.py sqlreset authors | perl -p -i -e 's/(DROP TABLE.*);/$1 CASCADE;/' | psql kultprosvet

And to create superuser from pgsql console. Thanks!
Reply all
Reply to author
Forward
0 new messages