Hi everyone,
I'm trying to get an account app with custom authentification and
registration.
Admin user can access to django admin site, simple user with required
permission can add new users through a CreateView and a form.
But, I get this quite annoying error message which tell me 'CreateView is
missing a QuerySet' . I have no idea how to fix it.
I understand from the doc that queryset reference model objects and that if
model is alreay define, then it's useless, i feel wrong here ...
Can someone explain me clearly what are those queryset and help me fixing
my code below ?
#models.py
class UserProfileManager(BaseUserManager):
def create_user(self, password=None, **kwargs):
user = self.model(**kwargs)
user.set_password(password)
user.save(using=self.db)
return user
def create_superuser(self, password=None, **kwargs):
user = self.create_user(password=password)
user.is_admin = True
user.save(using=self.db)
return user
class UserProfile(AbstractBaseUser):
__MAX_LENGTH = 100
username = models.CharField(max_length=__MAX_LENGTH, verbose_name="Nom
d'utilisateur", unique=True)
first_name = models.CharField(max_length=__MAX_LENGTH,
verbose_name="Prénom", default="")
last_name = models.CharField(max_length=__MAX_LENGTH, verbose_name="Nom",
default="")
email = models.EmailField(max_length=__MAX_LENGTH, verbose_name="Adresse
e-mail", default="")
perm_on_user = models.BooleanField(verbose_name="Gestion des utilisateurs",
default=False)
perm_on_news = models.BooleanField(verbose_name="Gestion de la newsletter",
default=False)
is_admin = models.BooleanField(verbose_name="Administrateur", default=False)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['first_name', 'last_name', 'email', 'perm_on_user',
'perm_on_news', 'is_admin', ]
object = UserProfileManager()
class Meta:
verbose_name = "Utilisateur"
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
#admin.py
class UserCreationForm(forms.ModelForm):
password = forms.CharField(label='Mot de passe', widget=forms.PasswordInput)
password_confirm = forms.CharField(label='Confirmation du mot de passe',
widget=forms.PasswordInput)
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'email', 'perm_on_user',
'perm_on_news', 'is_admin',)
def clean_password_confirm(self):
# Check that the two password entries match
password = self.cleaned_data.get("password")
password_confirm = self.cleaned_data.get("password_confirm")
if password and password_confirm and password != password_confirm:
raise forms.ValidationError("Les mots de passes ne correcpondent
pas")
return password
def save(self, commit=True):
# Save the provided password in hashed format
user = super().save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
class UserModificationForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = forms.CharField(label='Mot de passe', widget=forms.PasswordInput)
password_confirm = forms.CharField(label='Confirmation du mot de passe',
widget=forms.PasswordInput)
class Meta:
model = UserProfile
fields = ('first_name', 'last_name', 'email', 'perm_on_user',
'perm_on_news', 'is_admin',)
def clean_password_confirm(self):
# Check that the two password entries match
password = self.cleaned_data.get("password")
password_confirm = self.cleaned_data.get("password_confirm")
if password and password_confirm and password != password_confirm:
raise forms.ValidationError("Les mots de passes ne correcpondent
pas")
return password
def save(self, commit=True):
# Save the provided password in hashed format
user = super().save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserModificationForm
add_form = UserCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('username', 'first_name', 'last_name', 'email',
'perm_on_user', 'perm_on_news', 'is_admin',)
list_filter = ('perm_on_user', 'perm_on_news', 'is_admin')
fieldsets = (
(None, {'fields': (
'username', 'password', 'password_confirm', 'first_name', 'last_name',
'email', 'perm_on_user', 'perm_on_news',
'is_admin',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'first_name', 'last_name', 'email',
'password', 'password_confirm', 'perm_on_user',
'perm_on_news', 'is_admin'),
}),
)
search_fields = ('username',)
ordering = ()
filter_horizontal = ()
admin.site.register(UserProfile, UserAdmin)
admin.site.unregister(Group)
#view.py
class CreateUserView(CreateView):
model = UserProfile
fields = ['first_name', 'last_name', 'email', 'perm_on_user',
'perm_on_news', 'is_admin', ]
Thanks you for helping me !
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/6b15b7c4-cf2b-48e7-8ebe-e44c7f99b65a%40googlegroups.com.