执行以下操作时出现上述错误。
class UserViewSet(generics.ListCreateAPIView):
"""
A viewset for viewing and editing user instances.
"""
models = User
serializer_class = UserSerializer
queryset = User.objects.filter(is_active=1) # See this
我的模型确实包含is_active
字段,而序列化程序具有fields = '__all__'
。
我需要重写get_queryset
方法吗?
编辑:
以下作品。 编辑 not working now.
def get_queryset(self):
return User.objects.filter(is_active=1)
def get_queryset(self):
return self.queryset.filter(is_active=1)
def get_queryset(self):
queryset = super(UserViewSet, self).get_queryset()
return queryset.filter(is_active=1)
三个人都说 Cannot resolve keyword 'is_active' into field
。
P.S-以上三项都在queryset = User.objects.all()
课堂上尝试过
Models.py
class User(models.Model):
id = models.BigIntegerField(primary_key=True)
login_id = models.CharField(unique=True, max_length=64, blank=True, null=True)
is_active = models.CharField(max_length=1, blank=True, null=True)
auth_type = models.ForeignKey(AuthType, models.DO_NOTHING)
user_type = models.ForeignKey('UserType', models.DO_NOTHING)
user_org = models.ForeignKey('Organization', models.DO_NOTHING, blank=True, null=True)
parent = models.ForeignKey('self', models.DO_NOTHING, blank=True, null=True, related_name="childs")
auth_user_id = models.OneToOneField(AuthUser, models.DO_NOTHING, db_column="auth_user_id", related_name='cpeUser')
user_create_date = models.DateTimeField(blank=True, null=True)
user_expiry_date = models.DateTimeField(blank=True, null=True)
created_by = models.CharField(max_length=64, blank=True, null=True)
create_date = models.DateTimeField(blank=True, null=True)
updated_by = models.CharField(max_length=64, blank=True, null=True)
update_date = models.DateTimeField(blank=True, null=True)
class Meta:
managed = False
db_table = 'cpe_user'
答案 0 :(得分:1)
尝试将类属性设置为
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
然后覆盖如下的get_queryset
方法:
def get_queryset(self):
return self.queryset.filter(is_active=1)
希望这会有所帮助!