I have inherited UpdateView and I am trying to override the get_queryset() method, but it keeps throwing an error:
*raise AttributeError( AttributeError: Generic detail view UserProfileManagement must be called with either an object pk or a slug in the URLconf. *
I am trying not to use slugs etc. in my urls.py.
models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=50, blank=False, null=False)
last_name = models.CharField(max_length=50, blank=False, null=False)
start_date = models.DateTimeField(default=timezone.now)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['first_name', 'last_name']
objects = CustomUserManager()
def save(self, *args, **kwargs):
self.first_name = self.first_name.capitalize()
self.last_name = self.last_name.capitalize()
return super(User, self).save(*args, **kwargs)
def __str__(self):
return self.email
managers.py
class CustomUserManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password, **extra_fields):
if not email:
raise ValueError(_('The Email must be set'))
email = self.normalize_email(email)
user = self.model(email=email,
first_name=first_name,
last_name=last_name,
**extra_fields)
user.set_password(password)
user.save()
return user
(create_superuser deliberately omitted)
urls.py
(I do not want to use slugs etc.)
app_name = 'users'
urlpatterns = [
path('login/', auth_views.LoginView.as_view(), name='login'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('account/', views.UserProfileManagement.as_view(), name='profile'),
]
views.py
class UserProfileManagement(UpdateView):
model = User
template_name = 'users/user_profile.html'
fields = ['first_name', 'last_name']
def get_queryset(self):
queryset = super(UserProfileManagement, self).get_queryset()
return queryset.filter(pk=3)
However, if I override the get_object() instead it works.
def get_object(self, queryset=None):
return User.objects.get(pk=3)
CodePudding user response:
You wrote Model
instead of model
:
class UserProfileManagement(UpdateView):
model = User # Here
template_name = 'users/user_profile.html'
fields = ['first_name', 'last_name']
def get_queryset(self):
queryset = super(UserProfileManagement, self).get_queryset()
return queryset.filter(pk=3)