Django How to add loop column in model from another model

I want to add each City name in models A data to models B table Column name. When I add a new City is it possible to add and update the model B table?

class ModelA(models.Model):
    city_name = models.CharField(max_length=50)

    def __str__(self):
        return f"{self.city_name}"

class ModelB(models.Model):
    for eachCity in ModelA:
        code = eachCity.city_name
        code = models.CharField(max_length=200, blank=True, null=True)

        def __str__(self):
            return f"{self.options}"

you have to do this in your views.py, when you add city, get models B and update it and save, models is for creating database not for Logic. Or you can add some function in your model and use it in view when you add city.

Back to Top