I would like to use in_bulk() on a query like:
{{{
Data.objects.filter(foo='a').in_bulk(<list of bar values>,
field_name='bar').
}}}
Obviously, the fact that bar is not unique should not be an issue to run
the query, but Django performs a check to make sure the queried field is
unique and raise an error if not.
As it may not be simple to check for all cases where field_name would
result in a unique constraint, maybe it should be possible to disable this
check (using an optional parameter).
--
Ticket URL: <https://code.djangoproject.com/ticket/29764>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Comment (by Tim Graham):
In your example, if bar isn't unique, then values may be overwritten in
the `in_bulk()` return value unless the keys become `('foo', 'bar')` (but
how would `in_bulk()` know that?). I think this proposal would complicate
`in_bulk()` a bit too much.
--
Ticket URL: <https://code.djangoproject.com/ticket/29764#comment:1>
Comment (by Jean-Daniel):
Replying to [comment:1 Tim Graham]:
> In your example, if bar isn't unique, then values may be overwritten in
the `in_bulk()` return value unless the keys become `('foo', 'bar')` (but
how would `in_bulk()` know that?). I think this proposal would complicate
`in_bulk()` a bit too much.
In my example, bar is guarantee to be unique, as the query already
contains an equality filter on foo. And yes, I understand it would make
the function too complex to try to handle all possibles cases, that's why
I suggested to let the caller choose to disable the check, ie something
like this:
{{{
in_bulk(<list of bar>, field_name='bar', disable_unique_check=True)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/29764#comment:2>
Comment (by Simon Charette):
Given `in_bulk` can be easily replaced with a dict literal I don't think
it's worth complexifying it more for the rare cases where this is useful
either.
{{{#!python
bulk_data = {
data.bar: data for data Data.objects.filter(foo='a')
}
--
Ticket URL: <https://code.djangoproject.com/ticket/29764#comment:3>
* status: new => closed
* resolution: => wontfix
* type: Bug => New feature
--
Ticket URL: <https://code.djangoproject.com/ticket/29764#comment:4>