listAPI视图Django Rest框架中以10为底的int()的无效文字 [英] invalid literal for int() with base ten in listAPI view django rest framework

查看:83
本文介绍了listAPI视图Django Rest框架中以10为底的int()的无效文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在django rest框架中使用视图。在此视图中,需要一个参数 city 来获取该城市附近的列表。

I am using a view in django rest framework. In this view it takes an argument city to then fetch a list a neighborhoods in that city.

网址如下所示:

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

网址代码如下:

url(r'neighborhood-list/(?P<city>[a-zA-Z]+)/', VenueFilterOptionsView.as_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)

我不明白我是否将查找字段设置为city,除非仅适用于实例而不适用于列表?即便如此,我还是使用 listAPIView 通用

what I don't understand is I set the lookup field to city, unless that only works for instances not lists? And even so I am using the listAPIView generic

例外位置在这里:

    /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行上的代码如下:

and the code on line 966 is the following:

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类型。因此,我想现在的问题是我们如何克服这种废话或解决它。

the return value of this method in the init folder being referenced by the stack trace is being cast as an int every time. SO I guess now the question is how do we override this nonsense or work around it.

所以现在我在努力寻找正在发生的事情。

so now I am working my way back trying to figure out what is going on.

有人有什么想法吗?

推荐答案

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

Update - My original answer was incorrect. List view doesn't actually work with the lookup_field and lookup_url_kwarg attributes, those attributes are used by Rest Frameworks DetailView in the get_object(self) method to retrieve a single instance using those lookup fields.

我已经更新了答案,因此它覆盖了 get_queryset(self)方法以返回正确过滤的列表。

I've updated the answer so it overrides the get_queryset(self) method to return the correctly filtered list. This is how ListView should be customised.

似乎没有正确定义ListView。

It looks like you haven't defined your ListView properly. The problem seems to be that you are trying to filter on the Cities Primary Key, which is an integer field, using a string that can't be parsed as an integer. I'll write up how I think your view should look presuming your trying to do your filtering based on some field on the City model.

# 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',
    )
]

这将按城市名称过滤您的邻里如果您想按城市过滤邻里ry Key,那么您应该使用:

This will filter your Neighbourhoods by the Cities name. If you want to filter Neighbourhoods by the cities Primary Key, then you should use:

# 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',
    )
]

此选项可修复视图和url,以按城市主键过滤邻居。这样可以提高性能,因为它不需要在City和Neighbourhood之间进行联接。

This fixes the view and url's to filter Neighbourhoods by the Cities Primary Key. This would be more performant because it doesn't need to perform a join between City and Neighbourhood.

这篇关于listAPI视图Django Rest框架中以10为底的int()的无效文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆