M
M
Maqsat Batyrqul2017-04-14 09:07:48
Django
Maqsat Batyrqul, 2017-04-14 09:07:48

How can I get an object by ForeignKey?

serializers.serialize('json', Post.objects.filter(category=category_id))

So I get the object, in the response
{
  "success": true,
  "message": "Successful",
  "data": [
    {
      "model": "blog.post",
      "pk": 1,
      "fields": {
        "title": "Смерть перед жизнью",
        "category": 2,
        "content": "<p>fgasdf ewfwef qwefweqf wef wefwe</p>",
        "audio": "media/audio/2017/04/11/5.17.23_PM.ogg",
        "video": "media/video/2017/04/11/7M6ilfg.mp4",
        "author": 1,
        "preview": "media/preview/2017/04/12/default.jpg",
        "view_count": 0,
        "pub_date": "2017-04-11T11:10:38Z"
      }
    }
}

Is it possible to get the author not only id but the full object from the Author model? I tried like this https://docs.djangoproject.com/en/1.10/topics/seri...
but it doesn't work

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-14
@Maqsat

You forgot to add a couple of arguments to the serialize call:

serializers.serialize('json', Post.objects.filter(category=category_id),
    use_natural_foreign_keys=True, use_natural_primary_keys=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question