Django/Wagtail Type error 'ModelBase' object is not iterable
I have a project with wagtail. I have created a custom model Admin named sorting. I want to show this model's data in API.
Here is the view in the database
Here is models.py
from django.db import models
from wagtail.models import Page, Orderable
from modelcluster.fields import ParentalKey
from blog.models import *
from wagtail.models import Page
from wagtail.admin.panels import PageChooserPanel, FieldPanel, InlinePanel
from django.utils.translation import gettext_lazy as _
from modelcluster.models import ClusterableModel
from modelcluster.fields import ParentalManyToManyField
class IDChoice(models.IntegerChoices):
Featured1 = 0, _('Featured1')
Featured2 = 1, _('Featured2')
Featured3 = 2, _('Featured3')
Featured4 = 3, _('Featured4')
Featured5 = 4, _('Featured5')
class Featured(ClusterableModel):
featured_name = models.IntegerField(default=IDChoice.Featured1,choices=IDChoice.choices, blank=True, null=False, help_text='Featured ID', unique=True)
panels = [
InlinePanel('featured_pages', label="Featured pages"),
FieldPanel('featured_name'),
]
class FeaturedPages(Orderable, models.Model):
"""
Orderable helper class, and what amounts to a ForeignKey link
to the model we want to add featured pages to (Featured)
"""
featured_page = models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
)
featured_item = ParentalKey(
Featured,
related_name='featured_pages',
null=True,
on_delete=models.CASCADE,
)
panels = [
FieldPanel('featured_page'),
]
def __str__(self):
"""String repr of this class."""
return self.featured_page
class Meta: # noqa
verbose_name = "Featured"
verbose_name_plural = "Featured Stories"
admin.py
from .models import *
from wagtail.contrib.modeladmin.options import (ModelAdmin, modeladmin_register,)
class SortingAdmin(ModelAdmin):
model = Featured
menu_lebel= "Sorting"
menu_icon = "placeholder"
menu_order = 290
add_to_settings_menu = False
exclude_from_explorer = False
list_display = ("featured_name","featured_pages")
search_field = ("featured_name","featured_pages")
modeladmin_register(SortingAdmin)
views.py
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import FeaturedPages
from .serializers import FeaturedSerializer
class FeaturedList(APIView):
def get(self, request):
featured =FeaturedPages.objects.all()
serializer= FeaturedSerializer(FeaturedPages, many = True)
return Response(serializer.data)
serializers.py
from rest_framework import serializers
from .models import Featured
class FeaturedSerializer(serializers.ModelSerializer):
class Meta:
model = Featured
fields = "__all__" #("featured_name","featured_pages")
urls.py
....
from rest_framework.urlpatterns import format_suffix_patterns
from sorting import views
...
urlpatterns = [
...
path("api/sorting/", views.FeaturedList.as_view()),
]
The error I am getting while I am fetching the URL
TypeError at /api/sorting/ 'ModelBase' object is not iterable Request Method: GET Request URL: http://127.0.0.1:8000/api/sorting/ Django Version: 4.0.6 Exception Type: TypeError Exception Value: 'ModelBase' object is not iterable Exception Location: C:\Users\Fathi\Envs\mgolpoenv\lib\site-packages\rest_framework\serializers.py, line 686, in to_representation Python Executable: C:\Users\Fathi\Envs\mgolpoenv\Scripts\python.exe Python Version: 3.10.5
What am I missing? And How I can show those fields in API?