-
-
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
Enhancement dont require pk strictly related #2745 #2754
Enhancement dont require pk strictly related #2745 #2754
Conversation
Great stuff. I'd rather we drop the We could reconsider this if we end up with a large amount of support for any other backends in particular that make a clear case for needing an |
Cool sounds good, I'll update it :). |
Perfect! |
Something odd going on at the moment with the travis results never returning. |
Gotcha, let me know if anything ends up needing to be modified :) and not a problem! |
Restarted build. It's currently failing to something unrelated to this PR, because of outdated |
Still failing, this branch needs to be rebased in order to fix the |
Rebased and pushed up the rebase which fixed the build on Travis in my fork but for some reason the rebase didn't propagate to the PR. Any ideas? |
Restarted the build but it didn't reflect changes in the actual matrix, although the branch is correctly updated. I'm gonna go ahead and merge this anyways, since opening another PR just to workaround whatever is going on in Travis seems like a bit too much for this change. @bleib1dj thanks again! |
…rictly_related Enhancement dont require pk strictly related #2745
Sweet np! :) |
The following items had a wrong href value: - Dont require pk strictly for related fields. (encode#2745, encode#2754) - Restrict integer field to integers and strings. (encode#2835, encode#2836)
Rationale
I implemented the enhancement with the
id_field
to provide users using non-django ORM backends the ability to still utilize the check originally provided byif obj.pk is None
, prior to providing a related reference. I believe this gives more flexibility but also potentially provides unnecessary functionality as alternative backends may not generate an identification value solely upon storage and could potentially have their identifier provided prior to save. If the backend does that this check would only serve to verify that the user properly assigned an identifier but would not provide the intended affect of verifying the object has been created. That being said, there is the potential for this to also be done with the django ORM as the following example illustrates:This goes against using
User.objects.create(...)
but could be done. In either case providing theid_field
gives the user the ability to choose, wherehasattr()
limits the check to only working when thepk
attribute is present.If this way of handling the enhancement is agreed upon I can add additional docs detailing the optional field and when to utilize it. Otherwise we can close out this PR and I can open one with my
hassattr()
modifications instead.Additional references
Per our discussion in #2745 I found a couple additional locations of
pk
attribute references.PrimaryKeyRelatedField
There's a reference in the
to_representation
method but I opted not to change it as I believe theSlugField
provides a way of achieving this functionality for a non-django ORM based backend.ManyRelatedField
There was a reference in the
get_attribute
method that I swapped out withself.id_field
which also required the definition ofself.id_field
within it's own__init__
method.I chose to define it there rather than up in the
Field
class as theManyRelatedField
is "Automagically" created in theRelatedField
and passed itskwargs
. So instead of giving users the ability to set anid_field
on every field, which would be unnecessary, this should reduce it to them only needing to set it on theHyperlinkedRelatedField
andHyperlinkedIdentityField
if they choose to, while still supportingmany=True
.