I have added wagtail CMS’s blog author in my ‘models.py’ also exposed it in API, but it’s showing like this in API
JavaScript
x
9
1
"blog_authors": [
2
{
3
"id": 1,
4
"meta": {
5
"type": "blog.BlogAuthorsOrderable"
6
}
7
}
8
],
9
Here’s the models.py
code
JavaScript
1
55
55
1
class BlogAuthorsOrderable(Orderable):
2
"""This allows us to select one or more blog authors from Snippets."""
3
4
page = ParentalKey("blog.AddStory", related_name="blog_authors")
5
author = models.ForeignKey(
6
"blog.BlogAuthor",
7
on_delete=models.CASCADE,
8
)
9
10
panels = [
11
# Use a SnippetChooserPanel because blog.BlogAuthor is registered as a snippet
12
SnippetChooserPanel("author"),
13
]
14
15
16
@register_snippet
17
class BlogAuthor(models.Model):
18
"""Blog author for snippets."""
19
20
name = models.CharField(max_length=100)
21
website = models.URLField(blank=True, null=True)
22
image = models.ForeignKey(
23
"wagtailimages.Image",
24
on_delete=models.SET_NULL,
25
null=True,
26
blank=False,
27
related_name="+",
28
)
29
30
panels = [
31
MultiFieldPanel(
32
[
33
FieldPanel("name"),
34
# Use an ImageChooserPanel because wagtailimages.Image (image property)
35
# is a ForeignKey to an Image
36
ImageChooserPanel("image"),
37
],
38
heading="Name and Image",
39
),
40
MultiFieldPanel(
41
[
42
FieldPanel("website"),
43
],
44
heading="Links"
45
)
46
]
47
48
def _str_(self):
49
"""String repr of this class."""
50
return self.name
51
52
class Meta: # noqa
53
verbose_name = "Blog Author"
54
verbose_name_plural = "Blog Authors"
55
How do I serialize like this show author name, website, image and id?
I tried to Serialize the BlogAuthor
JavaScript
1
10
10
1
class AuthorSerializer(serializers.ModelSerializer):
2
class Meta:
3
model = BlogAuthor
4
fields = (
5
"id",
6
"name",
7
"website",
8
"image",
9
)
10
And here is the API field
APIField("blog_authors", serializer=AuthorSerializer(many=True)),
When I runserver I got this error
JavaScript
1
13
13
1
AttributeError at /api/v2/pages/4/
2
Got AttributeError when attempting to get a value for field `name` on serializer `AuthorSerializer`.
3
The serializer field might be named incorrectly and not match any attribute or key on the `BlogAuthorsOrderable` instance.
4
Original exception text was: 'BlogAuthorsOrderable' object has no attribute 'name'.
5
Request Method: GET
6
Request URL: http://127.0.0.1:8000/api/v2/pages/4/?fields=*
7
Django Version: 4.0.6
8
Exception Type: AttributeError
9
Exception Value:
10
Got AttributeError when attempting to get a value for field `name` on serializer `AuthorSerializer`.
11
The serializer field might be named incorrectly and not match any attribute or key on the `BlogAuthorsOrderable` instance.
12
Original exception text was: 'BlogAuthorsOrderable' object has no attribute 'name'.
13
How I can perfectly serialize my blog author?
Advertisement
Answer
I think the problem is there is an intermediate model between your Blog and BlogAuthor – the BlogAuthorsOrderable that has the sort_order field. I don’t know how to get exactly what you are asking for but try changing your AuthorSerializer as follows. Hopefully that will give you enough information to figure out your next steps.
JavaScript
1
5
1
class AuthorSerializer(serializers.ModelSerializer):
2
class Meta:
3
model = BlogAuthorsOrderable
4
fields = '__all__'
5