Giter VIP home page Giter VIP logo

Comments (11)

axnsan12 avatar axnsan12 commented on May 10, 2024

Hello!

You probably have multiple serializers named NestedSerializer in different packages. Each serializer gets mapped to an OpenAPI named model according to its name. You could go to each serializer in question and explicitly give it a more distinctive name, or force it to be generated as an inline model (both will work if you only care about the documentation aspect).

from drf-yasg.

nicholasgcoles avatar nicholasgcoles commented on May 10, 2024

Hey thanks for the quick reply!

None of the serializers I am using have a nested serializer called "NestedSerializer". One of my serializers which aren't working looks like this:

class ClientSerializer(serializers.ModelSerializer):
    """
    Serializer for clients
    """
    items = ItemSerializer(many=True, read_only=True)

    class Meta:
        model = Client
        fields = ('id', 'user', 'items', 'company')

Is there something I am doing wrong? I tried to look at that documentation but I was unsure as what to do with the "ref_name" attribute?

Thanks again!

from drf-yasg.

axnsan12 avatar axnsan12 commented on May 10, 2024

Huh. If that is the case then this might be a bug.

For the ref_name attribute, you would add it to the serializer Meta class.

from drf-yasg.

nicholasgcoles avatar nicholasgcoles commented on May 10, 2024

Hey! I realise I had depth=3 in one of the serializers which seemed to cause an issue.

I set the depth=1 and that has fixed the problem and going to debug further on my side.

Thanks so much for the help!

from drf-yasg.

axnsan12 avatar axnsan12 commented on May 10, 2024

That shouldn't be a problem, or if it is, it should be fixed. So please do report if you find that it is a bug caused by this library.

from drf-yasg.

dilipMaharjan avatar dilipMaharjan commented on May 10, 2024

I just confirmed that if depth is greater than 1 in more than one serializer, it throws the mentioned error.

from drf-yasg.

axnsan12 avatar axnsan12 commented on May 10, 2024

Ok, I'll try to figure it out. Any code example (e.g. django-admin startproject with minimal changes) that shows the problem would be of great help, if it's not too hard!

from drf-yasg.

dilipMaharjan avatar dilipMaharjan commented on May 10, 2024

I tried to reproduce the error this morning but couldn't which means its probably the way models are related, if I will have time, I will try it again some time later.

from drf-yasg.

dilipMaharjan avatar dilipMaharjan commented on May 10, 2024

@axnsan12 This a example project to view the issue https://github.com/dilipMaharjan/drf_yasg_depth_issue

Steps:

  1. Clone the project
  2. Run docker-compose up
  3. In your browser, type localhost/docs/
    4.Change the depth to 1 in CSerializer and it works
    I hope this helps.

from drf-yasg.

axnsan12 avatar axnsan12 commented on May 10, 2024

Hello!

Thanks a lot for the example app, it's great! The problem seems to be caused by having ModelSerializers with implicitly generated NestedSerializers for their model's ForeignKey relations...

It can be worked around by defining nested fields explicitly on your ModelSerializer to indicate which serializer they should use.

The permanent fix will likely be to force all such automatic serializers to be output inline (e.g. no entry in the bottom Models section, no sub-model name when viewing the operation). If you want to keep the named/by-reference behaviour you will have to implement the solution mentioned above anyway.

from drf-yasg.

dilipMaharjan avatar dilipMaharjan commented on May 10, 2024

Thanks.

from drf-yasg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.