SlugRelatedField with DataclassSerializer
See original GitHub issueDescribe 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:
- Created 9 months ago
- Comments:12 (7 by maintainers)
@HansAarneLiblik please review and test #899. hopefully this will do the trick.
of course there are multiple ways of using this field. 😞 I had a gut feeling this may produce issues. will look into it.