DJANGO-通过连接两个表从模型中获取值

时间:2019-08-29 00:02:52

标签: python-3.x django-models

我正在尝试通过模型从一个表中获取所有数据,我可以从该表中获取所有数据,这行得通,但是我需要从另一个表中获取一个由外键映射到另一个表的数据,这意味着我在Parcela模型中有一个名为 ID_USUARIO 的字段,我想从另一个名为 FULLNAME <的表中获取具有实际查询值的所有数据/ em> (在Usuario模型中)。

我该怎么做?

Usuario模型

class Usuario(Model):
    ID_USER = models.AutoField(primary_key=True)
    FULLNAME = models.CharField(max_length=500)
    EMAIL = models.EmailField()
    PASSWORD = models.CharField(max_length=450)

宗座模型

class Parcela(Model):
    """docstring for Parcela."""

    ID = models.AutoField(primary_key=True)
    ID_USUARIO = models.ForeignKey(Usuario, on_delete=models.CASCADE)
    ID_PARCELA = models.BigIntegerField()
    A_DEAD_LEAVES = models.FloatField()
    A_SOIL_DEPTH = models.FloatField()
    A_LIGHT_FUEL = models.FloatField()
    A_DUFF = models.FloatField()
    A_MID_HEA_FUEL = models.FloatField()
    A_SOIL_ROCK_COLOR = models.FloatField()
    B_DOMINANT_VEG_TYPE = models.CharField(max_length=200)
    B_FCOV = models.FloatField()
    B_FOLIAGE_ALTERED = models.FloatField()
    B_FREQ_LIVING = models.FloatField()
    B_NEW_SPROUTS = models.FloatField()
    C_DOMINANT_VEG_TYPE = models.CharField(max_length=200)
    C_FCOV = models.FloatField()
    C_FOLIAGE_ALTERED = models.FloatField()
    C_FREQ_LIVING = models.FloatField()
    C_LAI_CHANGE = models.FloatField()
    D_DOMINANT_VEG_TYPE = models.CharField(max_length=200)
    ...

这是我正在做的查询

export = Parcela.objects.select_related('ID_USUARIO')

查询结果

ID,ID_USUARIO,ID_PARCELA,A_DEAD_LEAVES,A_SOIL_DEPTH,A_LIGHT_FUEL,A_DUFF,A_MID_HEA_FUEL,A_SOIL_ROCK_COLOR,B_DOMINANT_VEG_TYPE,B_FCOV,B_FOLIAGE_ALTERED,B_FREQ_LIVING,B_NEW_SPROUTS,C_DOMINANT_VEG_TYPE,C_FCOV,C_FOLIAGE_ALTERED,C_FREQ_LIVING,C_LAI_CHANGE,D_DOMINANT_VEG_TYPE,D_FCOV,D_GREEN_UNALTERAD,D_BLACK_BROWN,D_FREQ_LIVING,D_LAI_CHANGE,D_CHAR_HEIGHT,E_DOMINANT_VEG_TYPE,E_FCOV,E_GREEN_UNALTERAD,E_BLACK_BROWN,E_FREQ_LIVING,E_LAI_CHANGE,E_CHAR_HEIGHT,COORDINATE_X,COORDINATE_Y,DATE_CAPTURED,DATE_SAVED,PHOTO_1,PHOTO_2,PHOTO_3,PHOTO_4,PHOTO_5,SEVERITY
2,1,1,20.0,30.0,10.0,20.0,15.0,6.0,Cactus,12.0,11.0,13.0,3.0,Pasto,2.0,6.0,2.0,3.1,Arbol,1.2,14.0,8.0,4.0,0.5,2.0,Hongos,3.0,1.9,11.0,10.2,20.1,18.4,624811.32,665561.23,2019-08-27 16:56:20,2019-08-27 16:56:20,,,,,,Moderada
3,1,1,20.0,30.0,10.0,20.0,15.0,6.0,Cactus,12.0,11.0,13.0,3.0,Pasto,2.0,6.0,2.0,3.1,Arbol,1.2,14.0,8.0,4.0,0.5,2.0,Hongos,3.0,1.9,11.0,10.2,20.1,18.4,624811.32,665561.23,2019-08-27 16:56:20,2019-08-27 16:56:20,,,,,,Moderada
4,1,1,20.0,30.0,10.0,20.0,15.0,6.0,Cactus,12.0,11.0,13.0,3.0,Pasto,2.0,6.0,2.0,3.1,Arbol,1.2,14.0,8.0,4.0,0.5,2.0,Hongos,3.0,1.9,11.0,10.2,20.1,18.4,624811.32,665561.23,2019-08-27 22:46:52,2019-08-25 15:46:12,,,,,,Moderada
5,1,2,100.0,2.0,2.1,1.2,1.0,1.1,Margaritas,40.0,0.3,1.25,2.65,Pinos,2.0,6.0,2.0,3.1,Guayacan,1.2,14.0,8.0,4.0,0.5,2.0,Roble,3.0,1.9,11.0,10.2,20.1,18.4,624811.32,665561.23,2019-08-24 22:46:52,2019-08-28 15:46:12,,,,,,Alta
6,2,3,100.0,2.0,2.1,1.2,1.0,1.1,Cedro,40.0,0.3,1.25,2.65,Rosas,2.0,6.0,2.0,3.1,Savila,1.2,14.0,8.0,4.0,0.5,2.0,Eucalipto,3.0,1.9,11.0,10.2,20.1,18.4,624811.32,665561.23,2019-08-24 22:46:52,2019-08-28 15:46:12,,,,,,No Quemada

1 个答案:

答案 0 :(得分:0)

是的,您做得正确。必须使用select_related来加入django。您可以验证生成的查询

result = Parcela.objects.select_related('ID_USUARIO')
print(str(result.query))
# You can access ID_USARIO fields like this
print(result.first().ID_USARIO.FULLNAME)

要获取具有名称的宗座表中的行,可以执行reverse lookup。然后查询将是

result = Usuario.objects.get(FULLNAME='<name_to_filter>')
parcela_rows = result.parcela_set.all()