Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#16156 closed Bug (invalid)

Django overrides user-defined "User" model with its own

Reported by: dloewenherz@… Owned by: nobody
Component: contrib.auth Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX:

Description

I'm getting some very weird results when I name my model "User".

Here's the definition.

from django.db import models

class User(models.Model):
    email = models.EmailField()
    password = models.CharField(max_length=51)
    birthday = models.DateField()
    gender = models.BooleanField() # True => Male

Here's the ModelForm:

from django import forms

class RegistrationForm(forms.ModelForm):
    class Meta():
        model = User
        fields = ['email', 'password']
        widgets = {
                'email': forms.TextInput(attrs={'autocomplete': 'off'}),
                'password': forms.PasswordInput(attrs={'autocomplete': 'off'}) }

When I render this form in my template with "{{ form.as_p }}" I see the following help text for the password input: "Use '[algo]$[salt]$[hexdigest]' or use the change password form.".

Seems as though there is some weird override of the Django-defined User model and mine.

Attachments (0)

Change History (2)

comment:1 Changed 3 years ago by kmtracey

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

I'd guess you are importing Django's User somewhere before you specify the model User in the meta class for your RegistrationForm. I do not see what you are describing, using your model, and avoiding any import of User from `django.contrib.auth.models':

>>> from ttt.models import User
>>> import datetime
>>> User.objects.create(email="xyz@xyz.com", password="uhoh", birthday=datetime.date.today(), gender=True)
<User: User object>
>>> u = User.objects.all()[0]
>>> u
<User: User object>
>>> u.email
u'xyz@xyz.com'
>>> from django import forms
>>> class RegistrationForm(forms.ModelForm):
...     class Meta():
...       model = User
...       fields = ['email', 'password']
...       widgets = {
...               'email': forms.TextInput(attrs={'autocomplete': 'off'}),
...               'password': forms.PasswordInput(attrs={'autocomplete': 'off'})}
... 
>>> rf = RegistrationForm(instance=u)
>>> rf.as_p()
u'<p><label for="id_email">Email:</label> <input name="email" value="xyz@xyz.com" autocomplete="off" maxlength="75" type="text" id="id_email" /></p>\n<p><label for="id_password">Password:</label> <input autocomplete="off" id="id_password" type="password" name="password" maxlength="51" /></p>'
>>> 
Version 0, edited 3 years ago by kmtracey (next)

comment:2 Changed 3 years ago by anonymous

I actually just figured it out (I think). Without thinking, I put this all in an app called auth. I think that threw off the namespacing with the built-in Django auth, which I have in INSTALLED_APPS and I am using for the admin.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.