[Fixed]-'QuerySet' object has no attribute ERROR, trying to get related data on ManyToMany fields

34👍

filter returns a QuerySet (as you may have guessed), you want to do get instead

user1 = Subject.objects.get(id=1)

If the Subject does not exist you will get a Subject.DoesNotExist exception. There’s also the get_object_or_404 shortcut in django.shortcuts that is useful if you’re simply grabbing an object that is to be displayed in some way and you want to return a 404 if it is not available.

3👍

QuerySet.get() will either return a single model as given by the criteria passed by it, or it will raise an exception.

Leave a comment