How can I check my society models name contains city's id or not?

Viewed 50

I want to check my society's name contains the city's id and locality's id or not.

  • First I want to iterate each society name and get city id by locality id and check that city name is appended at the end of the society name or not if yes then update all the society names by removing the city name.

  • Second Then again I want to iterate each society name and get locality id and check the locality name is appended at the end of the society name or not if yes then update all the society names by removing the locality name.

society.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'

locality.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'

city.py

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'

Please help me!! I have searched for a solution for many days but still didn't find out.
Thanks in advance!

1 Answers

This should help you:

for s in societies:
    locality = s.locality.name
    city = s.locality.city.name
    # search for locality name in society name
    res = s.name.find(locality)
    If res != -1:
        len_of_name = len(locality)
        new_name = s.name[:res]+s.name[res+len_of_name:]
        s.name = new_name
    # search for city name in society name
    res = s.name.find(city)
    If res != -1:
        len_of_name = len(city)
        new_name = s.name[:res]+s.name[res+len_of_name:]
        s.name = new_name
    s.save()
Related