consultations on django models

1

I have a problem that I do not know how to solve it. I have three models in my app:

class Hecho(models.Model):
    codigo = models.CharField(max_length=1)
    hecho = models.CharField(max_length=100)

class Beneficiario(models.Model):
    tipoDocumento = models.CharField(max_length=150)
    numeroDocumento = models.IntegerField()
    nombre = models.CharField(max_length=150)

class HechoBeneficiario(models.Model):
    beneficiario = models.ForeignKey(Beneficiario)
    hecho = models.ForeignKey(HechoVictimizante)

As you can see the model HechoBeneficiario relates the other two models.

My problem is like through the beneficiary model can I get to the model made and paint this in a template?

    
asked by jhon1946 14.03.2017 в 03:19
source

1 answer

2

In your case, you have a ManyToMany relationship between Fact and Beneficiary. Ideally, you would define a field in Done, for example, in which you would put something of the style:

class Hecho(models.Model):
    codigo = models.CharField(max_length=1)
    hecho = models.CharField(max_length=100)
    beneficiarios = models.ManyToManyField('Beneficiario', through='HechoBeneficiario', symmetrical=False, related_name='beneficiarios')

In such a way that you would access the objects

Hecho.objects.all().filter(beneficiarios__numeroDocumento="xxxxxxx")
    
answered by 15.03.2017 / 09:32
source