Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eager load, when deleting in django admin. #43

Open
Nix3c3r opened this issue Feb 25, 2022 · 1 comment
Open

Eager load, when deleting in django admin. #43

Nix3c3r opened this issue Feb 25, 2022 · 1 comment

Comments

@Nix3c3r
Copy link

Nix3c3r commented Feb 25, 2022

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
@d184230
Copy link

d184230 commented Sep 16, 2022

+1
I have same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants