在表manytomany django中获得价值

时间:2019-01-26 03:00:10

标签: django get

嗨,我正在尝试在many to many表中获得相同的值,但我不知道该如何实现

这是我的模特:

class Raza(models.Model):
     Nombre = models.CharField(max_length=50)
     Origen = models.CharField(max_length=45)
     Altura = models.CharField(max_length=10)
     Peso = models.CharField(max_length=10)
     Esperanza_vida = models.CharField(max_length=10)
     Actividad_fisica = models.CharField(max_length=45)
     Recomendaciones = models.CharField(max_length=500)
     Clasificacion_FCI = models.ForeignKey(Clasificacion_FCI,null=True,blank=True,on_delete=models.CASCADE)
     Tipo_pelo = models.ManyToManyField(Tipo_pelo,blank=True)
     Caracteristicas_fisicas = models.ManyToManyField(Caracteristicas_fisicas,blank=True)
     Caracter = models.ManyToManyField(Caracter,blank=True)
     Ideal = models.ManyToManyField(Ideal,blank=True)
     Tamanio = models.ForeignKey(Tamanio,null=True,blank=True,on_delete=models.CASCADE)
     User = models.ManyToManyField(User,blank=True)

我正在使用Django提供的User模型

我不知道该怎么做

我想做类似的事情

table user 
id_usuario = 1
name = "Juan"

table raza
id_raza = 1 
name = "pitbull"

table user_raza
id_user_raza = 1
id_user = 1
id_raza = 1

1 个答案:

答案 0 :(得分:0)

请在python中用小写字母写类属性。

您的问题:

raza = Raza.objects.get(id=1)

tipo_pelos = raza.tipo_pelo.all()
for tp in tipo_pelos:
    print(tp.id)
...
...

Django为此提供了wonderful documentation。玩得开心。