Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin list edit

I encountered some weird behavior when using admin list_editable with a restricting custom manager. Each time I am trying to save list changes in admin, I get the message: Please correct the errors below.

Any suggestions on how to get rid of this error message?

Here is a minimal sample:

models.py

from django.db import models

class RestrictedManager(models.Manager):
    def get_queryset(self):
        return super(RestrictedManager, self).get_queryset().none()


class MyModel(models.Model):
    on = models.BooleanField()

    objects = RestrictedManager()
    all_objects = models.Manager()

admin.py

from django.contrib.admin import ModelAdmin, site
from models import MyModel

class MyModelAdmin(ModelAdmin):
    list_editable = ('on',)
    list_display = ('id', 'on',)

    def get_queryset(self, request):
        return MyModel.all_objects

site.register(MyModel, MyModelAdmin)

If you wonder, why am I using a none() default queryset, I don't. I used none() only to simplify the example. The issue occurs with any object filtered out by the default manager.

like image 729
Yossi Avatar asked Jun 05 '26 00:06

Yossi


1 Answers

I didn't tested this yet, but probably the problem is, that you've overridden the default manager.

From the Django docs: Default managers

If you use custom Manager objects, take note that the first Manager Django encounters (in the order in which they’re defined in the model) has a special status. Django interprets the first Manager defined in a class as the “default” Manager, and several parts of Django (including dumpdata) will use that Manager exclusively for that model. As a result, it’s a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_queryset() results in an inability to retrieve objects you’d like to work with.

So using your "UnrestrictedManager" first and your Custom Manager as second should do the trick.

like image 157
normic Avatar answered Jun 06 '26 14:06

normic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!