A few tips on your code:
def get_latest_chemo(self):
chemo = ChemoRegime.objects.filter(patient=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 :)
On Fri, Sep 1, 2017 at 7:22 AM, Lachlan Musicman <datakid@gmail.com> wrote:
> On 1 September 2017 at 14:57, James Schneider <jrschneider83@gmail.com>
> wrote:
>>
>> I'm guessing that ChemoRegime.regime_age() contains line 95.
>>
>> ChemoRegime defines the stop_date field with null=True and blank=True. I'm
>> guessing this is because patients may not have yet stopped treatment, so
>> they have no stop_date.
>>
>> With that in mind, self.stop_date may be None in Python (and Null in the
>> DB). Subtracting None from a real datetime.date object is not supported.
>>
>> Ensure that stop_date has a date value before doing date math with it.
>>
>> -James
>>
>
> I am so embarrassed. Thank you James, spot on.
>
> cheers
> L.
>
> --
> 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 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/CAGBeqiM-kY1br4bzb5yWVEOCnXhcYd3ZoV8bUey1nJoS1iNWuQ%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.
--
Melvyn Sopacua
--
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 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/CA%2Bgw1GUaMHkuwG9YU-G_7fLo3vWt-5xd7BW_%3DwwOYL_qeUGJiA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment