Custom admin field widgets in the newforms-admin branch

13 views
Skip to first unread message

leif

unread,
Jun 26, 2007, 8:04:25 PM6/26/07
to Django users
One of the stated goals of the newforms-admin branch (
http://code.djangoproject.com/wiki/NewformsAdminBranch ) is to allow
developers to specify custom widgets for particular fields in a model.
I have not been able to find documentation regarding this new feature.
Has it been implemented yet?

Thanks!

leif

unread,
Jun 26, 2007, 8:12:42 PM6/26/07
to Django users
By the way, I did find the hook in django/branches/newforms-admin/
django/contrib/admin/options.py for specifying the form field for a
given database field. But that's different than allowing a developer
to specify the widget for a given field in the model itself.


leif

unread,
Jun 27, 2007, 2:26:42 PM6/27/07
to Django users
Does anyone have information on this topic? I have a feeling I'm not
the only one who's wondering this. Thanks!

James Bennett

unread,
Jun 27, 2007, 2:47:09 PM6/27/07
to django...@googlegroups.com
On 6/27/07, leif <leifstr...@gmail.com> wrote:
> Does anyone have information on this topic? I have a feeling I'm not
> the only one who's wondering this. Thanks!

Your best bet is probably to read the source right now; when the
branch is complete there will undoubtedly be documentation showing the
sorts of things you can do with it, but at the moment the code is
still under development.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

leif strickland

unread,
Jun 27, 2007, 9:43:38 PM6/27/07
to Django users
After some digging around, it looks like the newforms-admin branch
does not allow widget declarations within the model class itself.
However, adding a custom widget is quite easy.

In the branch, almost all of the admin app functionality has been
moved out of the inner Admin class and into a separate class called
ModelAdmin. If you want to customize the admin app's behavior, simply
subclass ModelAdmin and override the default functionality.

For example, look at this sample ModelAdmin subclass:

-----

from django.contrib import admin

[...]

class ProfileOptions(admin.ModelAdmin):
list_display = ('display_name', 'headline','setup_date')
filter_horizontal = ('techniques', 'products', 'setup')
fields = (('Administrative Settings',{'fields':
('subscriber','active')}),('Contact Information',{'fields':
('phone_numbers','im_accounts','zip_code')}),('Photos',{'fields':
('primary_photo','secondary_photos','private_photos')}),)

def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'zip_code':
return ZipCodeField(**kwargs)
else:
return super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

def queryset(self, request):
return self.model.user_profiles.get_query_set()


adminsite.register(Profile,ProfileOptions)

----


list_display and fields are used exactly as they were in inner Admin
classes. filter_horizontal is simply a tuple of fields that will use
the javascript multiple-select filter.

The queryset() function allows you to specify which manager should be
used to obtain the change list objects.

Then there's formfield_for_dbfield(), a function that allows you to
override the default form field types that are returned for your
models' fields. In this example, the function returns a custom field
class (ZipCodeField) for any model field named "zip_code".

Alternatively, you could specify a custom widget like so:

if db_field.name == 'zip_code':
kwargs['widget'] = WIDGET
return super(ProfileOptions,self).formfield_for_dbfield(db_field,
**kwargs)

Finally, don't forget to register your ModelAdmin subclass for the
model.

You can find more information on the branch's homepage:

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

I hope this helps! If I am wrong about any of this, please post so
others don't repeat my mistakes.

Cheers,
LS


James Bennett

unread,
Jun 27, 2007, 9:51:01 PM6/27/07
to django...@googlegroups.com
On 6/27/07, leif strickland <leifstr...@gmail.com> wrote:
> After some digging around, it looks like the newforms-admin branch
> does not allow widget declarations within the model class itself.

Yeah. As you've discovered, one of the big points of newforms-admin is
to get as much stuff as possible out of the model -- it doesn't really
belong there, and that's why you use a ModelAdmin class and register
with an admin site.

Reply all
Reply to author
Forward
0 new messages