-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
Support serializing unsaved models with related fields. #2637
Support serializing unsaved models with related fields. #2637
Conversation
@@ -80,6 +80,13 @@ def run_validation(self, data=empty): | |||
data = None | |||
return super(RelatedField, self).run_validation(data) | |||
|
|||
def field_to_native(self, obj, field_name): | |||
# Without PK there is no way to determine any relations to the object | |||
if obj.pk is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check #2641 to make sure we don't make another mistake here.
As per comment there, this seems similar to #2641. |
@carltongibson Just to confirm, do you know if this change is acceptable? If so, could you let me know what I need to do get it in a ready state? I could look into #2641 and check whether the PRs conflict. |
Hey @mdentremont — short answer, No I don't know. :-) What we've got are two related issues (no PKs on unsaved instances) popping up at the same time with two different fixes. At first glance the fix here looks fine to me — and it's tested — so, is it the behaviour we want and if so have we caught all the places? #2641 does the same as your change here, basically adding a In terms of getting this ready... If you looked at #2641 and thought it through that would help. Is it a related issue that your use-case would bump into coming from the other direction? Is there an obvious test for it? Does your patch fix it already? Clear enough? Sound reasonable? |
My comment is to this line only: if obj.pk is None: When you write as this, are you sure there will always be a 'pk' in 'obj'? From issue #2638, you can see it's not the case for the other place. Please just confirm this. I'm not sure whether this is an issue here. |
@zhigang to solidify this, we could also ensure obj has a pk attribute before checking if pk is none? |
@carltongibson I could test my change to check whether it also covers #2641, in which case we could close the other PR. It's so odd that both of these PRs were created withing a day of each other, when this has been present since at least 2.4, if not earlier |
@mdentremont if this pull-request could cover issue #2638 , I'm OK to discard #2641 . |
My mistake, |
- In both ManyRelatedField, provide an empty return when trying to access a relation field if the instance in question has no PK (so likely hasn't been inserted yet) - Add relevant tests - Without these changes, exceptions would be raised when trying to serialize the uncreated models as it is impossible to query relations without a PK - Add test to ensure RelatedField does not regress as currently supports being serialized with and unsaved model
I've removed the field_to_native change from my commit, it should not have been added in the first place. |
@mdentremont : your patch doesn't cover the case in issue #2638 from my test. |
This looks good to me. I'll make a further review but believe this should be going in, in it's current state. |
I've reviewed #2638. The issue there was specifically about creating the URL for a the So that means this is good to go. Thanks for the input! |
…ed-with-relations Support serializing unsaved models with related fields.
when trying to access a relation field if the instance in question has
no PK (so likely hasn't been inserted yet)
serialize the uncreated models as it is impossible to query
relations without a PK