V
V
Vampre2019-11-29 19:04:22
Django
Vampre, 2019-11-29 19:04:22

What is the cause of error in django admin?

For the Post model, the default manager is overridden

#models.py
class ObjectsOnManager(models.Manager):
    def get_queryset(self):
        return super(ObjectsOnManager, self).get_queryset().filter(status='on')


class OnOffStatusModel(models.Model):
    ON = 'on'
    OFF = 'off'
    STATUS_CHOICES = (
        (ON, 'Показывать'),
        (OFF, 'Не показывать'),
    )
    status = models.CharField("Статус", max_length=15, choices=STATUS_CHOICES, default=ON)
    objects_on = ObjectsOnManager()
    objects = models.Manager()

    class Meta:
        abstract = True

class Post(OnOffStatusModel):
    # определяем поля
    ......

Further, in order to see all posts in the admin panel, I override the get_queryset method:
#admin.py
class PostAdmin(admin.ModelAdmin):
    form = PostImageControlForm
    fields = ('count', 'status',  'image',  'title', 'descript', 'body', 'main_post', )
    list_display = ('title', 'main_post', 'count',)
    list_editable = ('main_post', 'status')
    list_filter = ('category', 'main_post', 'status')

    def get_queryset(self, request):
        return Post.objects.all()

Now, when editing the model from the post list page (i.e. via list_editable)
, the form validation error "Please fix the error below." pops up, and no fields are highlighted. I dug around in the debugger and eventually found an error in the formset in the changelist_view
[{'id': ['Choose the correct option. Your variant is not among the allowed values.']}, {}, {}, {}, {}]
I can't figure out why it pops up and what needs to be fixed...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question