Hello,
I've played a lot of time with Django and it's a great tool for basic query but I don't figure out how to play with Django to do a query spanning between a lot of models.
This is the query I do to display all Animals and their corresponding Vaccines with Encounter.status 'in-progress' and the Immunization date is in the futur.
def current_with_futur_vaccines(self):
return (
Encounter.objects.filter(
status="in-progress").filter(
subject__immunizations__recorded__gte=datetime.now(),
)
.select_related("subject")
.prefetch_related("subject__immunizations", "location")
)
{% for immunization in object.subject.immunizations.all %}
{{ immunization }}
{% endfor %}
The things is when I want to list the Immunizations from the query I get all the Immunizations for this animal and not only the Immunizations that have to take place in the futur like I said in the query. I guess it's because of the .all() and what I need is more something like Encounter.subject.immunization_set()
This is the model
class Animal(models.Model):
name = models.CharField(max_length=250)
class Encounter(models.Model):
subject = models.ForeignKey(Animal, on_delete=models.PROTECT)
status = models.CharField(max_length=11)
class Vaccine(models.Model):
name = models.CharField(max_length=250)
class Immunization(models.Model):
subject = models.ForeignKey(
Animal, on_delete=models.PROTECT, related_name="immunizations"
)
recorded = models.DateTimeField(default=timezone.now)
vaccine = models.ForeignKey(Vaccine, on_delete=models.PROTECT)
Thanks for your help