django models: get list of id django models: get list of id django django

django models: get list of id


You can do this using values_list method.

blogs = Blog.objects.filter(author=author).values_list('id', flat=True)

See more at the Django queryset documentation.


Blog.objects.filter(author=author).values_list('id', flat=True)

values_list() gives a list of rows, each row a tuple of all of the fields you specify as arguments, in order. If you only pass a single field in as an argument, you can also specify flat=True to get a plain list instead of a list of tuples.


Blog.objects.filter(author=author).values_list('pk', flat=True)

Put pk instead id, just for best practices.