Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access ForeignKey data without making extra queries in Wagtail(django)

I have the following two classes in my app.models and i'm using the wagtail APIs to get the data as json

class AuthorMeta(Page):
    author=models.OneToOneField(User)
    city = models.ForeignKey('Cities', related_name='related_author')

class Cities(Page):
    name = models.CharField(max_length=30)

So, when I try /api/v1/pages/?type=dashboard.AuthorMeta&fields=title,city, it returns the following data:

{
    "meta": {
        "total_count": 1
    },
    "pages": [
        {
            "id": 11,
            "meta": {
                "type": "dashboard.AuthorMeta",
                "detail_url": "http://localhost:8000/api/v1/pages/11/"
            },
            "title": "Suneet Choudhary",
            "city": {
                "id": 10,
                "meta": {
                    "type": "dashboard.Cities",
                    "detail_url": "http://localhost:8000/api/v1/pages/10/"
                }
            }
        }
    ]
}

In the city field, it returns the id and meta of the city. How can I get the name of the city in the response here, without making an extra query? :/

I couldn't find any solution in the Documentation. Am I missing something?

like image 841
suneet Avatar asked Feb 09 '23 07:02

suneet


2 Answers

Use Django model property to return through the ForeignKey:

class AuthorMeta(Page):
    author=models.OneToOneField(User)
    city = models.ForeignKey('Cities', related_name='related_author')
    city_name = property(get_city_name)

    def get_city_name(self):
        return self.city.name

Check Term Property to better understand the concept

like image 143
Dhia Avatar answered Feb 12 '23 14:02

Dhia


In case you have the foreign key in a Streamfield, e.g. a PageChooserBlock, you can customize the api response by overwriting the get_api_representation of a block, as described in the example as provided here:

class CustomPageChooserBlock(blocks.PageChooserBlock):
    """ Customize the api response. """

    def get_api_representation(self, value, context=None):
        """ Return the url path instead of the id. """
        return value.url_path
like image 37
user42488 Avatar answered Feb 12 '23 13:02

user42488