Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Support serializing unsaved models with related fields.#2637

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
carltongibson merged 1 commit intoencode:masterfrommdentremont:topic/allow-serialize-unsaved-with-relations
Mar 8, 2015
Merged

Support serializing unsaved models with related fields.#2637

carltongibson merged 1 commit intoencode:masterfrommdentremont:topic/allow-serialize-unsaved-with-relations
Mar 8, 2015

Conversation

@mdentremont
Copy link
Contributor

  • In both RelatedField and 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

@lovelydinosaurlovelydinosaur changed the titleAdd support for serializing models with related fieldsAdd support for serializing unsaved models with related fieldsMar 5, 2015
@lovelydinosaurlovelydinosaur changed the titleAdd support for serializing unsaved models with related fieldsSupport serializing unsaved models with related fields.Mar 5, 2015

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.

@carltongibsoncarltongibson mentioned this pull requestMar 5, 2015
@carltongibson
Copy link
Collaborator

As per comment there, this seems similar to#2641.

@mdentremont
Copy link
ContributorAuthor

@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.

@carltongibson
Copy link
Collaborator

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 aif obj.pk is None: ... guard clause at the begging of existing logic. — I'm not sure if doing that 3 (or more if there are other cases) times in the same filesmells or not. — Maybe it's okay, but I just flagged it to make sure we make the right change here.

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?

@ghost
Copy link

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.

@mdentremont
Copy link
ContributorAuthor

@zhigang to solidify this, we could also ensure obj has a pk attribute before checking if pk is none?

@mdentremont
Copy link
ContributorAuthor

@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

@ghost
Copy link

@mdentremont if this pull-request could cover issue#2638 , I'm OK to discard#2641 .

@mdentremont
Copy link
ContributorAuthor

@zhigang I've given this a test, and it allows me to serialize an unsaved model, where the HyperlinkedRelatedField is in place.

#2638 doesn't have enough code for me to test the use case; I was hoping maybe you could give it a shot with my changes?

@mdentremont
Copy link
ContributorAuthor

My mistake,field_to_native was removed in 3.0. It looks like not change was required forRelatedField (although the added test is nice to ensure this does not regress)

- 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
@mdentremont
Copy link
ContributorAuthor

I've removed the field_to_native change from my commit, it should not have been added in the first place.

@lovelydinosaurlovelydinosaur added this to the3.1.1 Release milestoneMar 6, 2015
@ghost
Copy link

@mdentremont : your patch doesn't cover the case in issue#2638 from my test.

@lovelydinosaur
Copy link
Contributor

This looks good to me. I'll make a further review but believe this should be going in, in it's current state.
Nice work!

@carltongibson
Copy link
Collaborator

I've reviewed#2638. The issue there was specifically about creating the URL for a theurl on aHyperlinkedModelSerializer. I've closed that given that a work around was already available usingvalidated_data, rather thandata for@zhigang's example.

So that means this is good to go. Thanks for the input!

carltongibson pushed a commit that referenced this pull requestMar 8, 2015
…ed-with-relationsSupport serializing unsaved models with related fields.
@carltongibsoncarltongibson merged commit53e1a2f intoencode:masterMar 8, 2015
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

Projects

None yet

Milestone

3.1.1 Release

Development

Successfully merging this pull request may close these issues.

3 participants

@mdentremont@carltongibson@lovelydinosaur

[8]ページ先頭

©2009-2025 Movatter.jp