On Friday, August 23, 2019 at 2:19:35 PM UTC+3, Benoit Dupont wrote:
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
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bd329d36-ce41-4a43-b5b1-d259a85d0759%40googlegroups.com.
No comments:
Post a Comment