1. Selecting Single Items
It looks like you're trying to get a single object. Using filter
will return a QuerySet
object (as is happening in your code), which behaves more like a list (and, as you've noticed, lacks the name
attribute).
You have two options here. First, you can just grab the first element:
info_l = info.objects.filter(id=1)[0]
You could also use the objects.get
method instead, which will return a single object (and raise an exception if it doesn't exist):
info_l = info.objects.get(id=1)
Django has some pretty good documentation on QuerySets, and it may be worth taking a look at it:
Docs on using filters
QuerySet
reference
2. Retrieving Specific Fields
Django provides the defer
and only
methods, which will let you choose specific fields from the database, rather than fetching everything at once. These don't actually prevent the fields from being read; rather, it loads them lazily. defer
is an "opt-in" mode, which lets you specify what fields should be lazily loaded. only
is "out-out" -- you call it, and only the fields you pass will by eagerly loaded.
So in your example, you'd want to do something like this:
info_l = info.objects.filter(id=1).only('name')[0]
Though with a model as simple as the example you give, I wouldn't worry much at all about limiting fields.