Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception: 'ManyRelatedField' object has no attribute 'Meta' #79

Closed
jayvdb opened this issue Jun 6, 2020 · 2 comments
Closed

Exception: 'ManyRelatedField' object has no attribute 'Meta' #79

jayvdb opened this issue Jun 6, 2020 · 2 comments

Comments

@jayvdb
Copy link
Contributor

jayvdb commented Jun 6, 2020

Describe the bug
(line numbers refer to master atm)

Traceback (most recent call last):
  File "./manage.py", line 14, in <module>
    main()
  File "./manage.py", line 10, in main
    execute_from_command_line(sys.argv)
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/management/commands/spectacular.py", line 44, in handle
    schema = generator.get_schema(request=None, public=True)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/generators.py", line 152, in get_schema
    paths=self.parse(request, public),
  File "/usr/lib/python3.8/site-packages/drf_spectacular/generators.py", line 132, in parse
    operation = view.schema.get_operation(path, path_regex, method, self.registry)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 76, in get_operation
    operation['responses'] = self._get_response_bodies()
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 790, in _get_response_bodies
    return {'200': self._get_response_for_code(response_serializers)}
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 815, in _get_response_for_code
    component = self.resolve_serializer(serializer, 'response')
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 887, in resolve_serializer
    component.schema = self._map_serializer(serializer, direction)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 560, in _map_serializer
    schema = self._map_basic_serializer(serializer, direction)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 614, in _map_basic_serializer
    schema = self._map_serializer_field(field, direction)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 397, in _map_serializer_field
    schema = self._map_serializer_field(field.child_relation, direction)
  File "/usr/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 409, in _map_serializer_field
    schema = self._map_model_field(field.parent.Meta.model._meta.pk, direction)
AttributeError: 'ManyRelatedField' object has no attribute 'Meta'

To Reproduce
WritableNestedModelSerializer is from https://github.com/beda-software/drf-writable-nested/blob/master/drf_writable_nested/serializers.py , but isnt related afaics.

class Foo(models.Model):
    name = models.CharField(max_length=255)


class Segment(models.Model):
    name = models.CharField(max_length=255)
    foo = models.ForeignKey(
        'Foo',
        on_delete=models.CASCADE,
        related_name='segments',
        null=True,
    )


class SegmentSerializer(WritableNestedModelSerializer):
    class Meta:
        model = Segment
        fields = ('id', 'name', 'foo')


class FooRetrieveSerializer(serializers.ModelSerializer):
    segments = serializers.PrimaryKeyRelatedField(
        read_only=True, many=True)

    class Meta:
        model = Foo
        fields = (
            'id',
            'name',
            'segments',
        )

Expected behavior
Should at least be an 'array of object', but ideally the type&schema of the object is also detected and exposed.

jayvdb added a commit to jayvdb/drf-spectacular that referenced this issue Jun 6, 2020
jayvdb added a commit to jayvdb/drf-spectacular that referenced this issue Jun 6, 2020
@tfranzel
Copy link
Owner

tfranzel commented Jun 7, 2020

i had a quick look. drf_writable_nested is indeed not the issue. i ran the example from the README and no exceptions, no warnings

i fixed the many related issue. it only occurred on read_only fields

@jayvdb please confirm and close.

@jayvdb
Copy link
Contributor Author

jayvdb commented Jun 7, 2020

Works well; thanks

@jayvdb jayvdb closed this as completed Jun 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants