DRF - Django Url过滤器 - RelatedFieldError

时间:2018-02-22 12:28:28

标签: python django django-rest-framework

更新: 结果证明是Django的版本冲突。我正在使用2.0,因为DUF当前版本兼容到1.11

我正在使用DUF作为过滤后端来处理DRF通用列表视图。

你可以帮助我解决这个错误吗?我确信我在这里做错了与相关领域有关。

当我尝试过滤网址时,我收到以下错误。我想过滤子模型的所有列。

http://127.0.0.1:8000/api/childlist/?customer_id=2

AttributeError at /api/childlist/
'OneToOneField' object has no attribute 'rel'

到目前为止,我的工作如下:

models.py

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Parent(models.Model):
    customer_id = models.BigIntegerField(primary_key=True)
    customer_name = models.CharField(blank=True, null=True, max_length=50)
    age = models.IntegerField(blank=True, null=True)


class Child(models.Model):
    customer_id = models.OneToOneField(Parent, on_delete=models.DO_NOTHING, related_name='customer_id_fk_parent')
    used_by = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='rel_user')
    comments = models.TextField(blank=True,null=True)

views.py

from rest_framework import generics

from onetoone.models import Child
from .serializers import Child_Serializer
from url_filter.integrations.drf import DjangoFilterBackend


#Required columns on Child -- All columns
FILTER_REQ_COLUMNS = [field.name for field in Child._meta.get_fields()]


class ChildList(generics.ListAPIView):
    queryset = Child.objects.all()
    serializer_class = Child_Serializer
    filter_backends = [DjangoFilterBackend]
    filter_fields = FILTER_REQ_COLUMNS

serializers.py

from rest_framework import serializers
from onetoone.models import Child


class Child_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Child
        exclude = []

urls.py

path('childlist/', ChildList.as_view(), name='api_child_list'),

当前列表数据如下

http://127.0.0.1:8000/api/childlist/

HTTP 200 OK
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "count": 3,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "comments": "1 is in use",
            "customer_id": 1,
            "used_by": 1
        },
        {
            "id": 2,
            "comments": "2 is in use",
            "customer_id": 2,
            "used_by": 1
        },
        {
            "id": 3,
            "comments": "3 in use",
            "customer_id": 3,
            "used_by": 1
        }
    ]
}

在gist中添加完整的追溯。

https://gist.github.com/just10minutes/b9add9c00ee3a14764b324ec30c65344

1 个答案:

答案 0 :(得分:1)

我重现了同样的错误,我认为,某种版本不匹配会导致错误。无论如何,我找到两种方法来避免错误,

方法-1
将django版本降级为1.11pip install django==1.11.10

方法-2
使用similae django过滤包,django-filter
1。安装包装
2.将django_filters添加到INSTALLED_APPS
3.重新定义view.py,如下所示

from django_filters import rest_framework as filters

FILTER_REQ_COLUMNS = [field.name for field in Child._meta.get_fields()]


class ChildList(generics.ListAPIView):
    queryset = Child.objects.all()
    serializer_class = Child_Serializer
    filter_backends = (filters.DjangoFilterBackend,)
    filter_fields = FILTER_REQ_COLUMNS



希望这能解决你的问题!