[Answered ]-Django Filter Results

2👍

✅

filter() returns a QuerySet() which is list-like and full of model objects. So even if there is only one result, you may need to pull it out of the returned list.

item = Lineclass.objects.filter()[0]

You may be better off using .get() if you expect there to be only one entry, this will also check your assumption, since it will throw an exception if more than one entry is returned. Maybe something like this:

try:
    item = Lineclass.objects.get(itemname__icontains=createprefabcons['newprefab'].cleaned_data['matlist'], dn1__icontains=createprefabcons['newprefab'].cleaned_data['sizelist'], lineclassname__icontains=createprefabcons['newprefab'].cleaned_data['matlist'])
except Lineclass.DoesNotExist:
    # handle case where no items are found
    do_stuff()
createprefabcons['newprefab'].cleaned_data['code'] = item

Leave a comment