Skip to content

Eager load, when deleting in django admin. #43

@Nix3c3r

Description

@Nix3c3r

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions