Edit admin list in Django

I ran into some strange behavior when using admin list_editable

with a limited custom manager. Every time I try to save my list changes in admin I get a message: Please correct below errors.

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

Here's 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're wondering why I'm using the none()

default queryset, I don't. I none()

only used to simplify the example. The problem occurs with any object filtered by the default manager.

+3


source to share


2 answers


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

In the Django docs: Default Managers



If you are using custom manager objects, note that the first column in Django Manager (in the order they are defined in the model) has a special status. Django interprets the first Manager defined in the class as the "default manager", and several parts of Django (including dumps) will use this Manager exclusively for this model. As a result, it is a good idea to be careful when choosing a default manager to avoid a situation where overriding get_queryset () makes it impossible to get the objects you would like to work with.

So, use your "UnrestrictedManager" first and your custom manager second to do the trick.

+1


source


I solved this problem by creating a proxy model with a simple manager.

class UnrestrictedMyModel(MyModel):
    objects = models.Manager()

    class Meta:
        proxy = True

site.register(UnrestrictedMyModel, MyModelAdmin)

      



But I am still looking for the best solution.

0


source







All Articles