Help organizing inline fields in User

543 views
Skip to first unread message

mondonauta

unread,
Nov 28, 2008, 8:00:36 AM11/28/08
to Django users
hello everybody,
i've already created a custom user profile
and added it to the User by
foreignkey, AUTH_PROFILE_MODULE
and i have been able to add the custom profile
to the admin page by coding in admin.py this:

___________________________________________
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin

from django.utils.translation import ugettext_lazy as _

from ProfSystem.users.models import Professor

admin.site.unregister(User)

# Set it up so we can edit a user's professor profile inline in the
admin
class ProfessorInline(admin.StackedInline):
model = Professor

class ProfessorAdmin(UserAdmin):
inlines = [ProfessorInline]

# re-register the User with the extended admin options
admin.site.register(User, ProfessorAdmin)
_____________________________________________

now my problem is that in this way the new fields
r in a new frameset at the bottom of the admin page!
while what i would like to do is adding the new fields
to the "Personal info" frameset.
is there any way to obtain this?

thanks for any one who is going to help me :-)

sergioh

unread,
Nov 28, 2008, 8:27:23 AM11/28/08
to Django users
You could use field sets to get your information organized:

fieldsets = (
('Account Information', { 'fields':('company_name', 'username',
'password', 'email', 'is_active')}),
('Company Information',{'fields': ('physical_address',
'city', 'state', 'zip_code', 'telephone', 'fax', 'type_of_business',
'years_in_business')}),
('Billing Information',{'fields': ('billing_address',
'billing_city', 'billing_state', 'billing_zip_code')}),
)

Regards,

Sergio Hinojosa

mondonauta

unread,
Nov 28, 2008, 9:48:41 AM11/28/08
to Django users
well... maybe i missed something, but i've already tried
what u told me.
i copied and pasted the fieldsets from
django.contrib.auth.admin.UserAdmin
to my ProfessorAdmin class and i modified it in order to
add my custom fields in the formset i wanted.

well what i got is this error:
__________________________________________
Exception Type: ImproperlyConfigured
Exception Value: 'ProfessorAdmin.fieldsets[4][1]['fields']'
refers to field 'phone_number' that is
missing from the form.
__________________________________________

so i guess this is not the way at least not in this case
where i want to add fields from a custom user profile
into a certain fieldset of the UserAdmin class.

considering that the original fieldsets in UserAdmin
class is:
___________________________________________

fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name',
'email')}),
(_('Permissions'), {'fields': ('is_staff', 'is_active',
'is_superuser', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login',
'date_joined')}),
(_('Groups'), {'fields': ('groups',)}),
)
___________________________________________

i would like to have something like:
___________________________________________
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name',
'email', 'phone_num', 'studio_address', 'site_url')}),
(_('Permissions'), {'fields': ('is_staff', 'is_active',
'is_superuser', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login',
'date_joined')}),
(_('Groups'), {'fields': ('groups',)}),
)
___________________________________________

with the fields 'phone_num', 'studio_address' and 'site_url'
from the Professor class (the custom user profile class)
into the 'Personal info' fieldset

mondonauta

unread,
Nov 29, 2008, 1:30:58 PM11/29/08
to Django users
is anyone able to tell me it it is possible and how?
pleasssssssse!!!

Adrian

unread,
Dec 13, 2008, 11:05:33 AM12/13/08
to Django users
On Nov 28, 3:48 pm, mondonauta <amoredir...@gmail.com> wrote:
> well what i got is this error:
> __________________________________________
> Exception Type:         ImproperlyConfigured
> Exception Value:        'ProfessorAdmin.fieldsets[4][1]['fields']'
>                                 refers to field 'phone_number' that is
>                                 missing from the form.
> __________________________________________
>

The UserChangeForm is missing the fields that you want to use in your
custom Inline. You have to create a new form and define those
fields. Also, the fieldset that you copied from UserAdmin needs to
have the _(...) removed from the first field of each tuple, which I've
done in the example below. I only defined 'phone_num' - you'll have
to define the rest of them.

from django.contrib.auth.forms import UserChangeForm

class ProfessorChangeForm(UserChangeForm):
phone_num = forms.CharField(label="Phone", max_length=15,
help_text = "Required. Use format xxx-xxx-xxxx. Phone numbers
only.",
error_message = "Please enter a valid phone number in format
xxx-xxx-xxxx'")

# repeat for studio_address, site_url, etc

class ProfessorAdmin(UserAdmin):
inlines = [ProfessorInline, ]
form = ProfessorChangeForm
fieldsets = (
(None, {'fields': ('username', 'password')}),
('Personal info', {'fields': ('first_name', 'last_name',
'email', 'phone_num', 'studio_address', 'site_url')}),
('Permissions', {'fields': ('is_staff', 'is_active',
'is_superuser', 'user_permissions')}),
('Important dates', {'fields': ('last_login',
'date_joined')}),
('Groups', {'fields': ('groups',)}),
)

admin.site.register(User, ProfessorAdmin)

Reply all
Reply to author
Forward
0 new messages