python - listAPI View 中以十为基数的int()的文字无效 djangorest框架

标签 python django django-rest-framework

我正在使用 django Rest 框架中的 View 。在此 View 中,它需要一个参数 city 来获取该城市的社区列表。

网址示例如下所示:

http://127.0.0.1:8000/api/neighborhood-list/chicago/

网址代码如下所示:

url(r'neighborhood-list/(?P<city>[a-zA-Z]+)/', VenueFilterOptionsView.as_view()),

View :

class NeighborhoodListView(generics.ListAPIView):
lookup_field = 'city'

def list(self, request, city):
    self.city = city
    queryset = Neighborhood.objects.filter(city=self.city)
    serializer = NeighborhoodSerializer(queryset, many=True)

序列化器:

class NeighborhoodSerializer(serializers.ModelSerializer):
    class Meta:
        model = Neighborhood
        fields = 'neighborhood'

型号:

class  Neighborhood(models.Model):
city = models.ForeignKey(City, null=True)
neighborhood = models.CharField(max_length=150, null=False)

我不明白的是我将查找字段设置为城市,除非这只适用于实例而不是列表?即便如此,我仍在使用 listAPIView 通用

异常位置在这里:

    /home/rickus/211hospitality/suitsandtables/backend/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 966

第966行的代码如下:

def get_prep_value(self, value):
        value = super(AutoField, self).get_prep_value(value)
        if value is None:
            return None
        return int(value)

堆栈跟踪引用的 init 文件夹中此方法的返回值每次都会被强制转换为 int。所以我想现在的问题是我们如何克服这些废话或解决它。

所以现在我正在努力弄清楚到底发生了什么。

有人有什么想法吗?

最佳答案

更新 - 我原来的答案不正确。 ListView 实际上不适用于 lookup_fieldlookup_url_kwarg 属性,这些属性由 Rest Frameworks DetailView 在 get_object(self) 方法中使用使用这些查找字段检索单个实例

我已经更新了答案,因此它会覆盖 get_queryset(self) 方法以返回正确过滤的列表。这就是 ListView 应该如何定制的。

<小时/>

看来您没有正确定义 ListView。问题似乎是您正在尝试使用无法解析为整数的字符串来过滤城市主键(这是一个整数字段)。我将写下我认为您的 View 应该是什么样子,假设您尝试根据城市模型上的某些字段进行过滤。

# models.py
class City(models.Model):
    name = models.CharField(max_length=32)

class Neighbourhood(models.Model):
    city = models.ForeignKey(City)

# views.py
class NeighbourhoodListView(generics.ListAPIView):
    queryset = Neighbourhood.objects.all()
    serializer_class = NeighbourhoodSerializer

    def get_queryset(self):
        return self.queryset.filter(city__name=self.kwargs.get('city')

# urls.py
urlpatterns = [
    url(
        r'^neighbourhood-list/(?P<city>[a-zA-Z]+)/$',
        NeighbourhoodListView.as_view(),
        name='neighbourhood-list',
    )
]

这将按城市名称过滤您的社区。如果您想按城市主键过滤社区,那么您应该使用:

# views.py
class NeighbourhoodListView(generics.ListAPIView):
    queryset = Neighbourhood.objects.all()
    serializer_class = NeighbourhoodSerializer

    def get_queryset(self):
        return self.queryset.filter(city=self.kwargs.get('city'))

# urls.py
urlpatterns = [
    url(
        r'^neighbourhood-list/(?P<city>\d+)/$',
        NeighbourhoodListView.as_view(),
        name='neighbourhood-list',
    )
]

这修复了按城市主键过滤邻域的 View 和网址。这会提高性能,因为它不需要在 City 和 Neighbourhood 之间执行连接。

关于python - listAPI View 中以十为基数的int()的文字无效 djangorest框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45339258/

相关文章:

python - 如何遍历一个对象并将它的所有属性分配给一个列表

python - Linux Python 服务器无法正常终止

jquery - 在Django中重新加载表数据而不刷新页面

Django rest_framework 自定义错误信息

Django 休息框架 : Set Permissions for function view

python - 使用 pip 将 Python 包安装到不同的目录中?

python - Django Python 脚本不输出文件

django - 是否有必要在 nginx.conf 中包含启用站点的目录?

python - 对于每组对 REST API 函数的调用,仅发送一封电子邮件

Django休息框架: Best practices?