Skip to content

schema and docs got error in recursive serializers #7560

@aliyazdi75

Description

@aliyazdi75

Checklist

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

I have a recursive model Category and all things are fine, except schema and docs
Model:

class Category(models.Model):
    foursquare_id = models.CharField(max_length=50, unique=True)
    name = models.CharField(max_length=50)
    plural_name = models.CharField(max_length=50)
    short_name = models.CharField(max_length=50)
    date_created = models.DateTimeField(auto_now_add=True)
    parent_category = models.ForeignKey('self', related_name='categories', blank=True, null=True,
                                        on_delete=models.CASCADE)
    owner = models.ForeignKey(Account, related_name='categories', default=1, on_delete=models.CASCADE)

    def save(self, *args, **kwargs):
        super(Category, self).save(*args, **kwargs)

Serializer:

class CategorySerializer(serializers.HyperlinkedModelSerializer):
    owner = BasicAccountSerializer(read_only=True)

    class Meta:
        model = Category
        fields = ['url', 'id', 'name',
                  'plural_name', 'short_name', 'date_created', 'owner', 'categories']

    def get_fields(self):
        fields = super(CategorySerializer, self).get_fields()
        fields['categories'] = CategorySerializer(many=True)
        return fields

When I go to schema and docs views as bellow I get an error and the server stops:

urlpatterns = [
    ...
    path('api/schema/', get_schema_view(title=API_TITLE)),
    path('api/docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION)),
]
Error
web_1  | Thread 0x00007f7cbd6ae700 (most recent call first):
web_1  |   File "/usr/local/lib/python3.8/socket.py", line 669 in readinto
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 182 in handle_one_request
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 172 in handle
web_1  |   File "/usr/local/lib/python3.8/socketserver.py", line 720 in __init__
web_1  |   File "/usr/local/lib/python3.8/socketserver.py", line 360 in finish_request
web_1  |   File "/usr/local/lib/python3.8/socketserver.py", line 650 in process_request_thread
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 870 in run
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 932 in _bootstrap_inner
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 890 in _bootstrap
web_1  | 
web_1  | Current thread 0x00007f7cbdeaf700 (most recent call first):
web_1  |   File "/usr/local/lib/python3.8/asyncio/tasks.py", line 37 in current_task
web_1  |   File "/usr/local/lib/python3.8/site-packages/asgiref/sync.py", line 351 in get_current_task
web_1  |   File "/usr/local/lib/python3.8/site-packages/asgiref/local.py", line 54 in _get_context_id
web_1  |   File "/usr/local/lib/python3.8/site-packages/asgiref/local.py", line 83 in _get_storage
web_1  |   File "/usr/local/lib/python3.8/site-packages/asgiref/local.py", line 103 in __getattr__
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/translation/trans_real.py", line 356 in gettext
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/translation/__init__.py", line 92 in gettext
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 140 in __text_cast
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/text.py", line 17 in capfirst
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 140 in __text_cast
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 152 in __cast
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 164 in __eq__
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/field_mapping.py", line 52 in needs_label
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/field_mapping.py", line 76 in get_field_kwargs
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 1208 in build_standard_field
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 1184 in build_field
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 1053 in get_fields
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 349 in fields
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/functional.py", line 48 in __get__
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 81 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 100 in list_repr
web_1  |   File "/usr/local/lib/python3.8/site-packages/rest_framework/utils/representation.py", line 84 in serializer_repr
web_1  |   ...
web_1  | 
web_1  | Thread 0x00007f7cbe870700 (most recent call first):
web_1  |   File "/usr/local/lib/python3.8/selectors.py", line 415 in select
web_1  |   File "/usr/local/lib/python3.8/socketserver.py", line 232 in serve_forever
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/servers/basehttp.py", line 216 in run
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 139 in inner_run
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 53 in wrapper
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 870 in run
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 932 in _bootstrap_inner
web_1  |   File "/usr/local/lib/python3.8/threading.py", line 890 in _bootstrap
web_1  | 
web_1  | Thread 0x00007f7ccc2a9740 (most recent call first):
web_1  |   File "/usr/local/lib/python3.8/pathlib.py", line 1194 in stat
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 379 in snapshot_files
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 359 in tick
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 319 in run_loop
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 313 in run
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 598 in start_django
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/utils/autoreload.py", line 613 in run_with_reloader
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 103 in run
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 96 in handle
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 371 in execute
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 61 in execute
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 330 in run_from_argv
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395 in execute
web_1  |   File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401 in execute_from_command_line
web_1  |   File "/opt/project/manage.py", line 17 in main
web_1  |   File "/opt/project/manage.py", line 21 in <module>
kojaberam_web_1 exited with code 250
Aborting on container exit...
I know that a possible fix is:
class CategoryCategoriesField(serializers.HyperlinkedModelSerializer):
    owner = BasicAccountSerializer(read_only=True)

    class Meta:
        model = Category
        fields = ['url', 'id', 'name',
                  'plural_name', 'short_name', 'date_created', 'owner', 'categories']


class CategorySerializer(serializers.HyperlinkedModelSerializer):
    owner = BasicAccountSerializer(read_only=True)
    categories = CategoryCategoriesField(many=True, read_only=True)

    class Meta:
        model = Category
        fields = ['url', 'id', 'name',
                  'plural_name', 'short_name', 'date_created', 'owner', 'categories']

But I can't use categories serializer for the third children.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions