Home > OS >  django 'str' object has no attribute 'id' in getlist()
django 'str' object has no attribute 'id' in getlist()

Time:04-08

I have this function that I tried to use to perform a delete operation on some items

def return_buk(request):
    books = Books.objects.all()
    selected = request.POST.getlist('marked_delete')
    for book in books:
        for select in selected:
            if select.book_id_id == book.id:
                print(select.id)

However, when I try to access the attributes, I get an error like this. Obviously the items in the getlist are coming as in str format. How can I get their attributes?

'str' object has no attribute 'id'

Model where marked_delete is coming from

class Issue(models.Model):
    borrower_id = models.ForeignKey(Student,on_delete=models.CASCADE)
    book_id = models.ForeignKey(Books,on_delete=models.CASCADE)

CodePudding user response:

Give this a try

selected_list = request.POST.getlist('marked_delete')
selected = Issue.objects.filter(id__in=selected_list)

for book in books:
    for select in selected:
        if select.book_id_id == book.id:
            print(select.id)
  • Related