Skip to content

Question/Assistance - django-url-filter-relatedfielderror #56

@rajesh-h

Description

@rajesh-h

I am working on DRF generic listview with DUF as filter backend.

Could you please assist me to get around this error? I am sure i am doing something wrong here with related to related fields.

When i try to filter on url I get the following error. I would like to filter on all columns of child model.

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

AttributeError at /api/childlist/
'OneToOneField' object has no attribute 'rel'
Below is my work so far:

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'),
Current list data as below

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
        }
    ]
}

fulltrace back is here
https://gist.github.com/just10minutes/b9add9c00ee3a14764b324ec30c65344

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions