0

我有下面的模型,我如何获得给定的邮政编码city id

from django.db import models

class City(models.Model):
    # ...
    pass

class Location(models.Model):
    # ...
    city= models.ForeignKey(City)

class Postcode(models.Model):
    # ...
    location = models.ForeignKey(Location)
    postcode = models.CharField(max_length=10)
4

1 回答 1

4

你可以这样做::

post_codes = Postcode.objects.filter(location__city__pk = city_id)

您可以在此处阅读有关跨越关系的查找的更多信息。可以跨越外__键可以是任何级别的深度

于 2013-11-11T19:02:45.560 回答