On 1 September 2017 at 16:13, Melvyn Sopacua <m.r.sopa...@gmail.com> wrote:

> A few tips on your code:
>
>     def get_latest_chemo(self):
>         chemo = ChemoRegime.objects.filter(pat
> ient=self).latest('stop_date')
>
>     class ChemoRegime(models.Model):
>         patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
>
> Use the related manager here. To make it readable, use:
>
>     class ChemoRegime(models.Model):
>         patient = models.ForeignKey(Patient, on_delete=models.CASCADE,
> related_name='chemo_regimes')
>
>     class Patient(models.Model):
>         def get_latest_chemo(self):
>             return self.chemo_regimes.latest('stop_date')
>
> When providing properties for use in templates, make them properties -
> makes the template more readable:
>
>     @property
>     def latest_chemo(self):
>          return self.chemo_regimes.latest('stop_date')
>
>     {{ patient.latest_chemo }}
>
> Similarly you can abstract active regimes:
>
>     class ChemoRegime(models.Model):
>          @property
>          def is_active(self):
>               return self.stop_date is None
>
>     {% if chemo.is_active %}<span class="badge
> badge-primary">Current</span>{% endif %}
>
>
> Of course, latest_chemo, should really be this if
> active and non-active regimes can be mixed:
>
>     @property
>     def has_active_chemo(self):
>          return self.chemo_regimes.filter(stop_date__isnull=True).count()
> > 0
>
>     @property
>     def latest_chemo(self):
>          if not self.chemo_regimes.count():
>               return None
>          if self.has_active_chemo:
>               return self.chemo_regimes.latest('start_date')
>          return self.chemo_regimes.latest('stop_date')
>
> Hope this helps :)
>


Melvyn - have only just got around to implementing your suggestions.
Excellent, worked a treat. Thank you for the new tricks!

Cheers
L.







------
"The antidote to apocalypticism is *apocalyptic civics*. Apocalyptic civics
is the insistence that we cannot ignore the truth, nor should we panic
about it. It is a shared consciousness that our institutions have failed
and our ecosystem is collapsing, yet we are still here — and we are
creative agents who can shape our destinies. Apocalyptic civics is the
conviction that the only way out is through, and the only way through is
together. "

*Greg Bloom* @greggish https://twitter.com/greggish/
status/873177525903609857

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAGBeqiOt%3DQGLWfxYxeYW2ajogpoSoZQLib3r727Bvsx3FB6AEg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to