简体   繁体   中英

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)

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',  ...]>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM