[Answered ]-How to get ids of the objects from django model?

0๐Ÿ‘

โœ…

Try this

qs = Society.objects.all()
citys = City.objects.values_list('name', flat=True)
data = []
for name in citys:
    response = qs.filter(name__iendswith=name)
    if response.exists():
        data.append(response)
print(data)

Or You can do something like this (i will recommend this)

import operator
from django.db.models import Q
from functools import reduce

qs = Society.objects.all()
citys = City.objects.values_list('name', flat=True)
clauses = (Q(name__iendswith=name) for name in citys)
query = reduce(operator.or_, clauses)
data = qs.filter(query)

1๐Ÿ‘

you can try something like this

qs = Society.objects.all()
citys = City.objects.all().values_list('name', flat=True)
qs = qs.filter(name__endswith=list(citys))
๐Ÿ‘คrahul.m

Leave a comment