Home > database >  how can I check that societies name contain city or locality name and update the society name?
how can I check that societies name contain city or locality name and update the society name?

Time:03-03

I have data of societies in my database but there are some societies that contain city names and locality names. I want to check whether the society name contains the city name and locality name and remove them or update the society name.

models\locality.py

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'

models\society.py

class Locality(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.
    city = models.ForeignKey('City', models.DO_NOTHING, db_column='cityId', blank=True, null=True, related_name='locality_set')  # Field name made lowercase.
    connect_database_id = models.IntegerField(db_column='connectDatabaseId', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'localities'

It will clear your doubts: Iterate through societies that do have names and for each society. If the issue of appending city & locality exists for the society. Remove city name & locality name from society's name and update society name.
e.g.
Society name in database: Lodha Downtown Dombivli East Thane

In the above example, the society name is 'Loadha Downtown' but it also contains the locality name 'Dombivli East' and city name 'Thane'.

How can I check that societies name contain the city or locality name through a loop or any iteration?

Please help me! Thanks.

CodePudding user response:

Check if any society name contains substring which is a name of a Locality using values_list()

for checking if it contains, we shall use icontains and loop through each locality_names_list with reduce function of python

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

locality_names_list = list(Locality.objects. values_list('name', flat=True)
society_names_contain_locality = Society.objects.filter(reduce(operator.and_, (Q(name__icontains=name) for name in locality_names_list)))
print(society_names_contain_locality) 

society_names_contain_locality return a list of society that you want, you can iterate through it to update or delete.

for updating you can use update() or save():

for society in society_names_contain_locality:
    society.update(name="???") #or society.delete() if delete

or if you want to handle all of them at the same time with only 1 value:

society_names_contain_locality.update(name="???")
society_names_contain_locality.delete() #warn: this delete all of them
  • Related