1

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'
10
  • Can you please show the City and Society model code Commented Mar 16, 2022 at 5:56
  • @Thierno Amadou Sow Yes, because I want to get each name by its id and compare it with society's name. As you can see in for loop. Commented Mar 16, 2022 at 6:00
  • 2
    this cityID = City.objects.values_list('pk',flat=True) will give you all cities's id, Commented Mar 16, 2022 at 6:24
  • @rahul.m Yes of course, check I have added Commented Mar 16, 2022 at 6:24
  • @Thierno Amadou Sow Thanks for your answer, So how can I add this to for loop as mentioned above in the post? Because when I'm adding this throws me an error: AttributeError: 'QuerySet' object has no attribute 'name' Commented Mar 16, 2022 at 6:29

2 Answers 2

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))
Sign up to request clarification or add additional context in comments.

1 Comment

this returning me this <QuerySet []>
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)

4 Comments

Yes this is working but it also prints <QuerySet []> for those cities which are not in society's name. There is any way to skip those city names which are not in society's name and only prints society name that containg city name.
@AkashSingh i have update my answer and try it.
one more doubt you can help me with it. I also want to remove the city name from the society name and save it into the database. How can I do this? I used remove and update but I didn't find any solution. please help me.
@AkashSingh could you please ask an other question separately and explain your problem i will try to help you if i can.explain it explicitly i am not good in english.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.