Because select_related() only works on ForeignKeys that are not null or blank, when you are customizing the admin, even if you set "list_select_related=True" you can still end up with way too many querys in the Admin changelist. By adding this code to your model you can decrease the queries dramatically.
I found I needed this when I was working with Django apps with a lot of legacy data and I couldn't set the ForeignKey null=False.
1 2 3 4 5 6 7 8 9 10 11 12
class SelectRelatedManager(models.Manager): def get_query_set(self): return super(SelectRelatedManager, self)\ .get_query_set()\ .select_related('organization') class SomeModel(models.Model): ... organization = models.ForeignKey(Organization, blank=True, null=True) .... objects = SelectRelatedManager()
More like this
- FileField having auto upload_to path by junaidmgithub 1 month ago
- LazyPrimaryKeyRelatedField by LLyaudet 1 month, 1 week ago
- CacheInDictManager by LLyaudet 1 month, 1 week ago
- MYSQL Full Text Expression by Bidaya0 1 month, 1 week ago
- Custom model manager chaining (Python 3 re-write) by Spotted1270 1 month, 2 weeks ago