Django在另一个模型中使用来自两个不同模型的字段

时间:2016-10-28 14:43:40

标签: django django-models

我的模型如下:

class City(models.Model):
    name = models.CharField(max_length=20, blank=False) 
    pin = models.IntegerField(null=False, )
    def __str__(self):
        return u"%s" % (self.name)
class Localities(models.Model):
    name = models.CharField(max_length=50, blank=False,)
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    def __str__(self):
        return u"%s, %s" % (self.name, self.city)

class Hall(models.Model):
    name = models.CharField(max_length=30, blank=False, verbose_name="Name of the Marriage Hall", )
    city = models.ForeignKey(City, on_delete=models.CASCADE, )
    landmarks = models.CharField(max_length=50, blank=False, verbose_name="Landmarks of the Marriage Hall", )
    seating_capacity = models.IntegerField(null=False, verbose_name="Seating capacity of the Marriage Hall",)
    ac = models.BooleanField(null=False, verbose_name="Is the Marriage Hall AC?",)
    garden_lounge = models.BooleanField(null=False, verbose_name="Is the Marriage Hall garden_lounge?",)
    avg_user_rating = models.DecimalField(max_digits=10, null=False, decimal_places=5, verbose_name="Average user rating of the Marriage Hall",)
    comments = models.CharField(max_length=50, blank=False, verbose_name="Customer comments for this Marriage Hall", )
    rent = models.DecimalField(max_digits=10, decimal_places=5, blank=True, )
    def __str__(self):
        return u"%s, %s" % (self.name, self.city)

这给了我如下可浏览的API: enter image description here

我想要的是我的大厅模型中的地方,但是当我选择一个特定的城市时,应填充与该城市相关的地区。

我怎样才能做到这一点?我该如何修改我的模型?

1 个答案:

答案 0 :(得分:0)

我使用'django_smart_select'来解决我的问题。