Short answer: under certain conditions, yes.
When building LEFT JOINs with a GenericForeignKey, Django calls GenericRelation.get_extra_restriction , which adds an additional condition to the ON clause with the restriction content_type_id.
For "ForeignKey" this method is also called by returning None.
You can use this place to add additional restrictions to the ON clause if you manage to organize your code to get the corresponding restriction parameters at a specific time.
class UserForeignKey(models.ForeignKey): def get_extra_restriction(self, where_class, alias, related_alias): field = self.model._meta.get_field('user') cond = where_class()
Sergey Tikhonov
source share