如何在Django中获取外键值?

时间:2014-09-03 01:39:50

标签: python django pycharm django-queryset

我有这三种模式:

ButuanMaps,所有者,LandProperty

class ButuanMaps(gismodel.Model):
    class Meta:
        verbose_name = u'Butuan Map'
        verbose_name_plural = u'Butuan Maps'

    clandpin = gismodel.CharField("Land PIN", max_length=50, 
                                   null=True, blank=True)
    ssectionid = gismodel.ForeignKey(Section)
    #ssectionid_id = gismodel.IntegerField()
    geom = gismodel.MultiPolygonField("Geom ID", srid=32651, 
                                   null=True, blank=True)
    objects = gismodel.GeoManager()

    def __unicode__(self):
        return self.clandpin


class LandProperty(models.Model):
    DEFAULT_OWNER_ID = 5
    class Meta:
        verbose_name = u'Land Property'
        verbose_name_plural = u'Land Properties'
    ctaxdec = models.CharField("Tax Declaration", max_length=50, 
                                null=True, blank=True)
    sgeomid = models.ForeignKey(ButuanMaps)

    def __unicode__(self):
        return self.sgeomid.clandpin


class Owner(models.Model):
    user= models.OneToOneField(User)
    spersonid = models.ForeignKey(Information)
    User.profile = property(lambda u: Owner.objects.get_or_create(user=u)[0])

    def __unicode__(self):
        return self.spersonid.cfirstname

我希望根据ButuanMapsOwner获取所有geom,并从LandProperty模型中获取每个geom中的信息。我试过这个查询集但是失败了: ButuanMaps.objects.filter(ssectionid=5).select_related('landproperty__owner')

0 个答案:

没有答案