django - 'QuerySet' object has no attribute ERROR, trying to get related data on ManyToMany fields -
i have following models:
class tag(models.model): tag_name = models.charfield(max_length=250) tagcat = models.foreignkey('tagcat') class subject(models.model): user = models.manytomanyfield(user) tags = models.manytomanyfield(tag) class tagcat(models.model): cat_name = models.charfield(max_length=100)
so have subject, has tag. want loop subjects , appropriate tags, trying construct right view. far, have:
def home(request): user1 = subject.objects.filter(id=1) print(user1.tags.all())
i expect tags of user through print statement, instead error
'queryset' object has no attribute 'tags'
how getting 'subject' objects respective tags , pass them template?
(ideally subjects. did 1 here, simplify process of troubleshooting)
filter
returns queryset
(as may have guessed), want get
instead
user1 = subject.objects.get(id=1)
if subject
not exist subject.doesnotexist
exception. there's get_object_or_404
shortcut in django.shortcuts
useful if you're grabbing object displayed in way , want return 404 if not available.
Comments
Post a Comment