Opened 6 years ago
Closed 6 years ago
#29764 closed New feature (wontfix)
Allow using QuerySet.in_bulk() with a composed unique field
Reported by: | Jean-Daniel | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 2.1 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
For instance, I have a model 'Data' with an unique_together constraint on (foo, bar) fields.
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).
Change History (4)
follow-up: 2 comment:1 by , 6 years ago
comment:2 by , 6 years ago
Replying to 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 wouldin_bulk()
know that?). I think this proposal would complicatein_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)
comment:3 by , 6 years ago
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.
bulk_data = { data.bar: data for data Data.objects.filter(foo='a') }
comment:4 by , 6 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Summary: | in_bulk does not works with composed unique field → Allow using QuerySet.in_bulk() with a composed unique field |
Type: | Bug → New feature |
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 wouldin_bulk()
know that?). I think this proposal would complicatein_bulk()
a bit too much.