请帮助理解。 我有一个REST API的django应用程序。一切正常,但我想通过client_id添加过滤器,在这里我收到了问题。
有错误消息
invalid literal for int() with base 10: 'B123456'
确定这是我的代码
api.py
class DatabaseByClientList(RetrieveUpdateDestroyAPIView):
serializer_class = DatabaseSerializer
def get_queryset(self):
client_id = self.kwargs['client_id']
return Database.objects.filter(client_id=client_id)
models.py
class Database(models.Model):
def __unicode__(self):
return unicode(self.id)
class Meta:
ordering = ['db_name']
client_id = models.ForeignKey(ClientInfo)
db_name = models.CharField(max_length=30, blank=True)
db_host = models.CharField(max_length=30, blank=True)
db_user = models.CharField(max_length=30, blank=True)
db_pass = models.CharField(max_length=30, blank=True)
db_type = models.CharField(max_length=30, blank=True)
serializer.py
class DatabaseSerializer(serializers.ModelSerializer):
client_id = serializers.SlugRelatedField(queryset=ClientInfo.objects.all(),
slug_field='access_id')
class Meta:
model = Database
fields = ('id', 'client_id', 'db_name', 'db_host', 'db_user', 'db_pass', 'db_type')
怎么了?我在文档中做了一切。 谢谢, PS我试过
return Database.objects.filter(client_id=client_id)
但它也没有帮助
添加了ClientInfo类
class ClientInfo(models.Model):
def __str__(self):
return self.access_id
class Meta:
ordering = ['id']
access_id = models.CharField(max_length=15, unique=True, blank=True)
username = models.CharField(max_length=15, unique=True, blank=True)
password = models.CharField(max_length=15, unique=True, blank=True)
name = models.CharField(max_length=15, unique=True, blank=True)
答案 0 :(得分:0)
尝试以这种方式覆盖get_queryset方法。
class DatabaseByClientList(...):
model = Database
serializer_class = DatabaseSerializer
def get_queryset(self):
qs = super(DatabaseByClientList, self).get_queryset()
qs.filter(client_id=self.kwargs['client_id'])
return qs