[django-users] Error when registering a new user - Django 1.5

603 views
Skip to first unread message

Leonardo S

unread,
Mar 22, 2013, 11:20:02 PM3/22/13
to django...@googlegroups.com
Hi,

I am getting an error when registering a new user in a Django 1.5 app.
It is the postgres' log:

BRT ERROR:  relation "auth_user" does not exist at character 280
BRT COMMAND:  SELECT "auth_user"."id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."username" = E'teste'

My code is very simple:

# models.py
class User(AbstractUser):
    bio = models.CharField(max_length=255, blank=True, null=True)
    objects = UserManager()

# forms.py
class UserCreateForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = ('email', 'password1', 'password2', 'first_name', 'last_name', 'bio', 'username')

# views.py
class UserCreateView(CreateView):
    form_class = UserCreateForm
    model = get_user_model()

I think that UserCreationForm yet search for auth_user table.
A possible solution would be this:

# models.py
class User(AbstractUser):
    bio = models.CharField(max_length=255, blank=True, null=True)
    objects = UserManager()
    class Meta:
         db_table = u'auth_user'

But i would like to use a correct Django 1.5 approach to register a new user.
Can anyone spot the problem?

Regards,
  Leonardo

Leonardo S

unread,
Mar 23, 2013, 12:00:40 AM3/23/13
to django...@googlegroups.com
I did it and aparently it worked:

# forms.py
class UserCreateForm(UserCreationForm):

    def clean_username(self):
        username = self.cleaned_data["username"]
        try:
            self._meta.model._default_manager.get(username=username)
        except self._meta.model.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])

    class Meta:
        model = get_user_model()
        fields = ('email', 'password1', 'password2', 'first_name', 'last_name', 'bio', 'username')

From: https://groups.google.com/forum/?fromgroups=#!topic/django-users/kOVEy9zYn5c
Should not it be the UserCreationForm standard?


2013/3/23 Leonardo S <leonard...@gmail.com>

Russell Keith-Magee

unread,
Mar 23, 2013, 9:05:20 PM3/23/13
to django...@googlegroups.com
Variations on this problem report have been made a couple of times

UserCreateForm is a ModelForm. That means the line in your Meta clause for model=get_user_model() is invoked at the time the module is loaded. However, there's no guarantee at the time that module is loaded that the user model has been defined yet -- so you can end up with errors.

There are two options here.

Option 1: We try to define a truly dynamic UserCreateForm that can adapt to the changing requirements of a User model (and remember, this means adapting to the REQUIRED_FIELDS as well). This code would be complex and potentially fragile, since it needs to work with *any* User model, with *any* collection of required fields.

Option 2: We provide a UserCreateForm that works with the default User model, and with any AbstractUser descendent that isn't too significantly modified, and we leave the UserCreateForm for any other User model an an activity for the developer. 

We've chosen Option 2, and documented as such:


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...@googlegroups.com.
To post to this group, send email to django...@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.
 
 

Reply all
Reply to author
Forward
0 new messages