question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SlugRelatedField with DataclassSerializer

See original GitHub issue

Describe the bug With version 0.25.0, our schema generation started to fail, because we use SlugRelatedField with DataclassSerializer not ModelSerializer, and therefore do not have field.parent.Meta.model field, but have provided queryset attr to SlugRelatedField constructor, which should be used to detect the type instead

Source of bug: https://github.com/tfranzel/drf-spectacular/commit/0307ff92029b5bf32ca42523e2e664a5405a18dc#diff-4d081c8c817b2246109e52886d6b1f078858a2d5c99ea6e694796e39a7d96d77R662 #893

To Reproduce

from dataclasses import dataclass

from django.db import models
from rest_framework import serializers
from rest_framework_dataclasses.serializers import DataclassSerializer

class MyModel(models.Model):
    name = models.CharField(max_length=200, unique=True)
    value = models.IntegerField()


@dataclass()
class MyBase:
    obj: MyModel


class MyBaseSerializer(DataclassSerializer):
    slug_name = serializers.SlugRelatedField(slug_field="name", source="obj", queryset=MyModel.objects)

    class Meta:
        dataclass = MyBase
        fields = ["slug_name"]

AttributeError: type object ‘Meta’ has no attribute ‘model’

Expected behavior model to be determined from provided queryset model = field.queryset.model if hasattr(field, "queryset") else getattr(field.parent.Meta, "model", None)

or perhaps try to deduct from dataclass field type ?

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tfranzelcommented, Dec 14, 2022

@HansAarneLiblik please review and test #899. hopefully this will do the trick.

1reaction
tfranzelcommented, Dec 14, 2022

of course there are multiple ways of using this field. 😞 I had a gut feeling this may produce issues. will look into it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializer relations - Django REST framework
SlugRelatedField. SlugRelatedField may be used to represent the target of the relationship using a field on the target. For example, the following serializer:...
Read more >
django - Creating and saving foreign key objects using a ...
You will need a custom SlugRelatedField that creates the new object when one doesn't exist. class CreatableSlugRelatedField(serializers.
Read more >
Dataclasses serializer for Django REST framework - GitHub
This serializer provides a shortcut that lets you automatically create a Serializer class with fields that correspond to the fields on a dataclass....
Read more >
SlugRelatedField with Django and the Rest framework - Medium
I have an ecommerce application. The frontend for this project was written in AngularJS. I wanted to refactor this application to use Django ......
Read more >
Tips and Tricks - Django REST Framework - SlugRelatedField
DRF tip: To represent the target of the relationship with one of its fields, you can use SlugRelatedField in the serializer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found