how does django iterate through a list?

i think your tag field takes tag = ‘apple, phone, tenis, telvision’ as a string. you can do this,

posts = []
items = ['apple', 'shoes', 'milk', 'blue', 'black', 'phone']
instance = Post.objects.all()
for item in items:
    for data in instance:
        if item in data.tag.split(','):
            posts.append(data)
            break

you will get all instance of your model which contains ‘items’ or get single object of model and do posts = []

data= Post.objects.get(id=1)
for item in items:
    if item in data.tag.split(','):
        posts.append(data)
        break

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top