Django: How to retrieve a particular item for specific user from a model?

How can I retrieve a specified field for a particular user in Django? I got user: A, B, C and user A got 10 url records in the database. The problem is how to retrieve the 10 url records of user A? Thank you so much.

class Bookmark(models.Model):
url = models.CharField(max_length=500)
shortlisted_by = models.ForeignKey(User, related_name='bookmark')
shortlisted_date = models.DateTimeField(auto_now_add=True)

1 answer

  • answered 2017-11-23 07:49 Ankita Gupta

    Use values_list() of queryset.

    urls = Bookmark.objects.filter(shorlisted_by=user_id).values_list('url', flat=true)
    # here user_id is id of user object
    # Output: <QuerySet['http://domian', 'http://newurl',  ...]>