python - DRF-Django URL 过滤器-RelatedFieldError

标签 python django django-rest-framework

更新: 原来是Django版本冲突。我使用的是 2.0,DUF 当前版本兼容到 1.11


我正在使用 DUF 作为过滤器后端开发 DRF 通用 ListView 。

您能帮我解决这个错误吗?我确信我在这里做错了与相关领域相关的事情。

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

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

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

以下是我迄今为止的工作:

模型.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

序列化器.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
        }
    ]
}

在要点中添加完整的跟踪。

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

最佳答案

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

方法1
将 django 版本降级至 1.11 (pip install django==1.11.10)

方法2
使用类似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



希望这能解决您的问题!

关于python - DRF-Django URL 过滤器-RelatedFieldError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48927439/

相关文章:

python - 如何计算系列对象( Pandas )内数据框的平均值?

Python:全局变量的两个输入源

python - 加入多处理超时

css - Django 静态文件链接

Django 使用数据库中的字段预填充表单

python - Django Admin Million Data - 管理页面打开时间太长

python - 如何自定义QComboBox

python - 处理唯一一起错误的最佳方法 - Django 2.2?

python - Django OAuth 工具包 - 注册用户

django - 调用create()时`TypeError`。您可能需要将字段设置为只读,或覆盖create()方法