Extending Django User model using add_to_class()

2,219 views
Skip to first unread message

pr

unread,
Jun 22, 2009, 5:01:42 PM6/22/09
to Django users
Hi,

Is it a good way to extend Django User model using add_to_class()?
I have to add only two extra fields to the User model and I think that
using Profile Model to do this is unnecessary.

My way:

# Accounts models.py
User.add_to_class('field1', models.CharField(max_length=255))
User.add_to_class('field2', models.CharField(max_length=255))

# Accounts admin.py

class CustomUserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('email',)}),
(_('Permissions'), {'fields': ('is_staff', 'is_active',
'is_superuser', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login',
'date_joined')}),
(_('Groups'), {'fields': ('groups',)}),
(_('MyExtraFields'), {'fields': ('field1', 'field2')}),
)

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)

Finally:
> python manage.py syncdb

Thank You,
regards.

James Bennett

unread,
Jun 22, 2009, 5:14:03 PM6/22/09
to django...@googlegroups.com
On Mon, Jun 22, 2009 at 4:01 PM, pr<cric...@gmail.com> wrote:
> Is it a good way to extend Django User model using add_to_class()?
> I have to add only two extra fields to the User model and I think that
> using Profile Model to do this is unnecessary.

No, it's a very bad method. Consider what happens if two people want
to add fields of the same name; trying to stick them in the User model
will obviously fail and break at least one person's code. Using the
standard method of a related profile model keeps them neatly
namespaced.

As a general rule, consider code in other people's applications
(including applications in django.contrib) to be read-only.


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

pr

unread,
Jun 22, 2009, 6:30:13 PM6/22/09
to Django users
On 22 Cze, 23:14, James Bennett <ubernost...@gmail.com> wrote:
> No, it's a very bad method. Consider what happens if two people want
> to add fields of the same name; trying to stick them in the User model
> will obviously fail and break at least one person's code.
Yes, I know what you mean, but It's small project with one programmer.
I want to know about speed and stability in production mode above
rules of 'programming-tao' :-)

regards.

James Bennett

unread,
Jun 22, 2009, 6:40:25 PM6/22/09
to django...@googlegroups.com
On Mon, Jun 22, 2009 at 5:30 PM, pr<cric...@gmail.com> wrote:
> Yes, I know what you mean, but It's small project with one programmer.
> I want to know about speed and stability in production mode above
> rules of 'programming-tao' :-)

Well, there's also the fact that:

1. Using a profile means relying on documented, guaranteed-stable
behavior in Django.
2. Using the add_to_class trick means relying on undocumented
internals which have no stability guarantee whatsoever, and which are
subject to change without warning.

Seriously: there's a documented and supported method for storing
additional information related to users. Please take advantage of it.

(and, really I speak from experience regarding other ways to do this;
I have hacked up Django in just about every way possible, and I
remember back when you could do things like outright replace models
with your own definitions, and all of it was a freaking nightmare for
maintainability)

Reply all
Reply to author
Forward
0 new messages