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

Unable to expand writable related fields on POST #24

Open
BerniWittmann opened this issue Aug 14, 2018 · 7 comments
Open

Unable to expand writable related fields on POST #24

BerniWittmann opened this issue Aug 14, 2018 · 7 comments

Comments

@BerniWittmann
Copy link

BerniWittmann commented Aug 14, 2018

I try to create an object via a POST request. This works very well, now to my problem:

I want the response to have the field expanded by default (in the direct response to the post request).
When trying this either via http://localhost:8000/api/cities?expand=country or setting extensions_expand = {'country'} on the view (my preferred way), i get a validation error "country": ["This field is required." ].

So using extensions_expand={'country'} results in an error on POST (and also PUT i think). I can bypass this error by using get_extensions_mixin_context on the view

 def get_extensions_mixin_context(self):
        context = super(CityViewSet, self).get_extensions_mixin_context()
        if self.request.method in ['POST', 'PUT']:
            context['expand'] = set()
        return context

This works at least, but still doesn't produce the desired result of having country expanded in the post response.

I also have a repo ready for reproduction: https://github.com/BerniWittmann/drf_serializer_ext
Just try to post something to localhost:8000/api/cities. The desired response would be:

{
        "url": "http://localhost:8000/api/cities/1/",
        "name": "Berlin",
        "id": 1,
        "country": {
            "url": "http://localhost:8000/api/countries/1/",
            "name": "Germany",
            "id": 1,
            "code": "DE"
        },
        "country_id": 1
}

If anything is unclear, let me know :)

@BerniWittmann BerniWittmann changed the title Problem with expanding on Post Problem with expanding writable fields on POST Aug 14, 2018
@evenicoulddoit evenicoulddoit changed the title Problem with expanding writable fields on POST Unable to expand writable related fields on POST Aug 14, 2018
@evenicoulddoit
Copy link
Owner

Hi Ben - I see, thanks for this. I can think of a couple work arounds for your situation, (e.g. recreate the serializer within the view), but I think a more involved fix isn't that trivial. Welcome any pull requests if you manage to look into it (you'd need to modify the serializers module)

@evenicoulddoit
Copy link
Owner

Hi @BerniWittmann - I realise this is over 2 years old, so apologies, and no worries if you've forgotten altogether, but I had a question.

Am I right to assume that you wanted to post the ID only of the country, but to receive in the response, the fully serialized country?

@BerniWittmann
Copy link
Author

Hey @evenicoulddoit, no worries :) I have forgotten about it either.

I guess that this was true indeed.

@nebiyuelias1
Copy link

I'm still struggling with this issue today.

Repository owner deleted a comment from nebiyuelias1 Feb 21, 2022
@evenicoulddoit
Copy link
Owner

@nebiyuelias1 can you tell me more?

@nebiyuelias1
Copy link

I am basically trying to do the same exact thing as @BerniWittmann.

@nebiyuelias1
Copy link

Adding the extensions_expand field to a view somehow makes a field you wanted to expand required somehow.

luca-vari added a commit to AnikaLegal/clerk that referenced this issue Jun 20, 2024
…PI response

Change from using a library (see commit 78f8c42)
for this to specifying the serializers by hand. The library was too buggy,
particularly the following issue but also others:

  evenicoulddoit/django-rest-framework-serializer-extensions#24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants