for example, as shown in the code, there are three tables, the city, the area, the house, the city to which the house belongs and the area to which the house belongs. I hope that when entering data in the background, I select the option that the city can automatically generate all areas of the city. But limit_choices_to only has the option of writing dead directly, such as writing 1 with id 1 and writing 2 with id 2, otherwise there will be no data.
is there any way to solve this problem?
from django.db import models
class City(models.Model):
name = models.CharField(max_length=10)
class Area(models.Model):
name = models.CharField(max_length=10)
belong_city = models.ForeignKey(City, on_delete=models.CASCADE)
class House(models.Model):
name = models.CharField(max_length=50)
house_belong_city = models.ForeignKey(City, on_delete=models.CASCADE)
house_belong_area = models.ForeignKey(Area,
on_delete=models.CASCADE,
limit_choices_to={"belong_city": house_belong_city.primary_key})