-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Nested Object Serialization in DRF-2.4.3 with "fields" yields crash on startup #1943
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
Comments
This is the same underlying issue as #1907 -- instantiating a serializer at module level (depending on exactly how the serializer is configured) may try to do model introspection work that isn't really safe to do at module import time (because the model system may not be fully setup yet). In previous Django versions this could have silently caused wrong results; in 1.7 it's now an outright error. The only solution that's come to my mind would be to make using nested serializers a bit more explicit by introducing a
You'd do this:
And then internally DRF would lazily instantiate the This could be done backwards-compatibly, with the old approach still supported but deprecated. I'd need to do quite a bit more poking around to determine how hard this would be (and how much 3.0 already changes things here; I haven't really looked at the 3.0 code much yet). @tomchristie what do you think? |
@carljm - Unsure. Other approach would be to lazily load |
@tomchristie That's a great idea; much less invasive. I've filed a pull request implementing it. I'm not sure how your release process works - I don't see any "stable" 2.x branches. But it would be awfully nice if this fix were backported into a 2.x bugfix release, if 3.0 is going to be a bigger backwards-incompatible release. (I still kind of like the idea of |
Oh nm, I see - it looks like master is the 2.x series, and 3.x has its own branches. So this PR being against master should already get it into the next 2.x release. |
Now resolved in master. |
Hello.
When I use a nested ModelSerializer that uses the "field" parameter to restrict fields, i'm getting a "AttributeError: 'Options' object has no attribute '_related_objects_cache'", and "Models aren't loaded yet" crash upon startup.
Please note i'm using Python 3.4.1, django 1.7, and DRF 2.4.3 (django and DRF are installed via pip) on MacOS 10.9.4
Example:
(model)
(serializers)
When launching django, i get:
If i change the UserProfileSerializer to use the not use the "fields" parameter, everything works fine. e.g.
Also, if i use the "exclude" variable to exclude the fields I don't want instead of using the "fields" parameter to include the fields I want, it works fine. e.g.
This was a big issue for me, but since I found a workaround, it's less urgent now.
The text was updated successfully, but these errors were encountered: