jmcarp / jmcarp/nplusone

Eager load, when deleting in django admin.

Open
#43 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.1k
Forks
54
PR merge metrics
No merged PRs in 30d

Description

I have a quite frustrating problem. After fixing a lazyload-issue I now can't delete this model.
Imagine a mixin along with its admin mixin. The ModelAdminMixin overrides `get_queryset` with a call to `select_related`; otherwise I'd get a N+1 warning when displaying the related object list or details. This however causes, that I can't delete any of these objects in the admin interface. I will get an eager loading error, wenn pressing delete or the history button of the object. How can I fix that?
In the following you see a boiled down implementation of the mixins and how they are used.
```
class ModelMixin(models.Model):
created_by = ForeignKey(settings.AUTH_USER_MODEL, on_delete=PROTECT, related_name="created_%(app_label)s_%(class)s")
class Meta:
abstract = True

class ModelAdminMixin(object):
def get_queryset(self, request):
return super(ModelAdminMixin, self).get_queryset(request).select_related('created_by')
```
They are then used in a concrete model as such:
```
# models.py
#------------

class ConcreteModel(ModelMixin):
name = models.CharField(_('name'), max_length=100, blank=False, default='undefined')

# admin.py
#-----------

@register(Project)
class ConcreteModelAdmin(ModelAdminMixin, admin.ModelAdmin):
pass
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.