Skip to content

Proof of Concept for single lookup on HyperlinkRelatedField - #5150

Closed
carltongibson wants to merge 1 commit into
encode:masterfrom
carltongibson:experiment/to_internal_value
Closed

carltongibson wants to merge 1 commit into
encode:masterfrom
carltongibson:experiment/to_internal_value

Conversation

@carltongibson

@carltongibson carltongibson commented May 17, 2017 •

Copy link
Copy Markdown
Collaborator

Ref #4917 and #5093 by @orf, hoping to advance the discussion there.

Proof of Concept. Do Not Merge

This just shows how a single-lookup for HyperlinkRelatedField would look like in ManyRelatedField.

There's no attempt to make it look pretty.

The idea (from #5093) is that RelatedField subclasses add a lookup_parameters parameters method returning a dict that looks ≈like:

{"pk":1} 

or

{"slug": "my-great-post"}

This is lookup_field and lookup_value.

Here ManyRelatedField maps a list of lookups to a single __in lookup and validates that the len of the requested and returned sets match.

If something like this were adopted to_internal_value on non-many fields would use lookup_parameters as-is to feed into the get call.

@carltongibson

Copy link
Copy Markdown
Collaborator Author

This is all quiet, so I expect there's no imminent follow-up. (That's OK).

Question: do we want to handle to this case in core? i.e. do we want to jump through the hoops to ensure a single lookup?

There are ≈6 RelatedField subclasses that would need some refactoring to this sort of pattern, plus tests and docs.

@lovelydinosaur

lovelydinosaur commented Jun 16, 2017 •

Copy link
Copy Markdown
Member

First pass: 🤷‍♂️ 💭 ?

Gonna need to get deeper into this another day.

@carltongibson

Copy link
Copy Markdown
Collaborator Author
  1. Do we want to optimise the O(n) lookup? (Ideally yes)
  2. In which case we can refactor into this sort of pattern. (It's a moderate amount of effort: happy to do that but want to be sure first.)

Was thinking about closing both of these off as I wasn't entirely happy with the correctness check. (Around ln 570) I'm worried about missing edge-cases and introducing hard to maintain errors there.

For me it's a question of "what's in scope?" now.

@lovelydinosaur

Copy link
Copy Markdown
Member

As things stand I'd probably push this into "known constraint" territory, unless we could simplify things. 🤷‍♂️

No great answers here.

@carltongibson

carltongibson commented Jul 10, 2017 •

Copy link
Copy Markdown
Collaborator Author

...unless we could simplify things

I know the POC here isn't that simple — in that I didn't tidy anything up — but the underlying idea is fairly straightforward (I think):

  1. Construct the lookup parameters
  2. Do the fetch.

This is what get_object already does. The idea here is just to explicitly separate these so that the Get Many version and manipulate the return from 1 in order to use an __in lookup.

A completed PR, on these two points, should be clean enough. (I'd hope)

The third step:

  1. Make sure the fetched data matches the requested data (or raise ValidationError)

... seems harder. A simple set comparison may be enough but I'm not (as yet) convinced there aren't all number of ways that may go wrong. (Though perhaps it is that simple...?)

🤷‍♂️

Exactly :-)

I wonder if it's worth making ManyRelatedField an attribute on RelatedField so subclasses could override the class returned from many_init, allowing user-defined behaviour here?

I haven't got the bandwidth to take this on right now. I'll close this and related for now. It's something I may return to (although I'm more likely to cache the lookup first so...)

@lovelydinosaur

Copy link
Copy Markdown
Member

Thanks @carltongibson - wish there was a more obvious best-way-forward here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants