Home > front end >  How to get ids of the objects from django model?
How to get ids of the objects from django model?

Time:03-16

How to get ids of the objects from the model.

qs = Society.objects.all()
cityID = City.objects.get(pk=1)

The above query will return only one object whose id is 1. but I need all cities id so that can I access the city's names from id as you can see in below code.

for s in qs:
...     s_name = qs.filter(name__iendswith=cityID.name)
...     break

Added: model society and city

class Society(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(db_column='createdAt', auto_now_add=True)  # Field name made lowercase.
    updated_at = models.DateTimeField(db_column='updatedAt', auto_now=True)  # Field name made lowercase.
    locality = models.ForeignKey('Locality', models.DO_NOTHING, db_column='localityId', blank=True, null=True, related_name='society_set')  # Field name made lowercase.
    dot_com_database_id = models.IntegerField(db_column='dotComDatabaseId', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'societies'

class City(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(db_column='createdAt', auto_now_add=True)  # Field name made lowercase.
    updated_at = models.DateTimeField(db_column='updatedAt', auto_now=True)  # Field name made lowercase.
    currency = models.CharField(max_length=255, blank=True, null=True)
    connect_database_id = models.IntegerField(db_column='connectDatabaseId', blank=True, null=True)  # Field name made lowercase.
    dot_com_database_name = models.CharField(db_column='dotComDatabaseName', max_length=255, blank=True, null=True)  # Field name made lowercase.
    location_master_request_url = models.CharField(db_column='locationMasterRequestUrl', max_length=255, blank=True, null=True)  # Field name made lowercase.
    country = models.ForeignKey('Country', models.DO_NOTHING, db_column='countryId', blank=True, null=True, related_name='city_set')  # Field name made lowercase.
    pms_operation_status = models.CharField(db_column='pmsOperationStatus', max_length=255, blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'cities'

CodePudding user response:

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))

CodePudding user response:

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)
  • Related