Thursday, February 28, 2013

Re: 1.5 custom user model: Add User with default UserCreationForm results in "no such table: auth_user"


On Thu, Feb 28, 2013 at 5:11 AM, Eric Psalmond <epsalmond@gmail.com> wrote:
"UserCreationForm

Depends on the User model. Must be re-written for any custom user model."

I swore I RTM'ed :).  Thanks!

I think it really doesn't depend on the User model - my class inherits from AbstractUser.  I think with the one modification I made, it works fine.  I think just a check in the form to see if AUTH_USER_MODEL is defined, and if so use that class instead of direct references to the User class would make it work 100% for any class that inherits from AbstractUser.

You'd think so, wouldn't you :-)
 
Internally, it's a little more complicated than that, because of the way that the currently defined User is determined at runtime. Due to the way that Python and Django load modules, there's no guarantee that the models you need will exist at the time the form is loaded, which can lead to some interesting circular dependencies. This is the major reason why UserCreationForm and UserChangeForm are bound to the User model specifically.

The good news is that you can still re-use the form logic in those two forms -- a form is just a class, so you can subclass them. If you User model subclasses AbstractUser, all the core fields have the same name and constraints, so all you need to do is redeclare the Meta portion of the class definition (to bind your subclass to your actual User model). This way you get all the logic for password/username checks, and the custom save() methods, but against your own model and field list.

Yours,
Russ Magee %-)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment