Django in_bulk() raising error with distinct()

Viewed 245

I have the following QuerySet:

MyModel.objects
.order_by("foreign_key_id")
.distinct("foreign_key_id")
.in_bulk(field_name="foreign_key_id")

foreign_key_id is not unique on MyModel but given the use of distinct should be unique within the QuerySet.

However when this runs the following error is raised:

"ValueError: in_bulk()'s field_name must be a unique field but 'foreign_key_id' isn't."

According to the Django docs on in_bulk here it should be possible to use in_bulk with distinct in this way. The ability was added to Django in response to this issue ticket here.

What do I need to change here to make this work?

I'm using Django3.1 with Postgres11.

1 Answers
Related