4

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?

Dhia
  • 10,119
  • 11
  • 58
  • 69
suneet
  • 400
  • 1
  • 5
  • 19

2 Answers2

6

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

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
Dhia
  • 10,119
  • 11
  • 58
  • 69
  • This works very well, but what about many-to-one relations. [Like this](http://stackoverflow.com/questions/36498417/how-to-access-manytooneforeignkey-data-without-making-extra-queries-in-wagtaild) – suneet Apr 08 '16 at 11:49
2

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
user42488
  • 1,140
  • 14
  • 26