如何在使用 UpdateView Django 时覆盖 get_queryset()
How to override get_queryset() whilst using UpdateView Django
我已经继承了 UpdateView 并试图重写 get_queryset() 方法,但它一直抛出错误:
*引发属性错误(
AttributeError:通用详细视图 UserProfileManagement 必须使用对象 pk 或 URLconf 中的 slug 调用。
*
我尽量不在 urls.py 中使用 slug 等。
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)
但是,如果我改写 get_object() 它会起作用。
def get_object(self, queryset=None):
return User.objects.get(pk=3)
你写的是 Model
而不是 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)
我已经继承了 UpdateView 并试图重写 get_queryset() 方法,但它一直抛出错误:
*引发属性错误( AttributeError:通用详细视图 UserProfileManagement 必须使用对象 pk 或 URLconf 中的 slug 调用。 *
我尽量不在 urls.py 中使用 slug 等。
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)
但是,如果我改写 get_object() 它会起作用。
def get_object(self, queryset=None):
return User.objects.get(pk=3)
你写的是 Model
而不是 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)