Weird date math bug?

28 views
Skip to first unread message

Lachlan Musicman

unread,
Aug 31, 2017, 11:54:41 PM8/31/17
to django...@googlegroups.com
Is there a weird date math bug in Django 1.11.4? I say weird because surely not?

I can't see any tickets in the tracker, but I'm definitely seeing it in production.

Using postgresql:


class Patient(models.Model):


------
"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

Lachlan Musicman

unread,
Sep 1, 2017, 12:45:08 AM9/1/17
to django...@googlegroups.com
Sorry, let me start again:



Is there a weird date math bug in Django 1.11.4? I say weird because surely not?

I can't see any tickets in the tracker, but I'm definitely seeing it in production.

Using postgresql:


models.py

class ChemoType(models.Model):
    name = models.CharField(max_length=50, unique=True)

class Patient(models.Model):
    chemo_regimes = models.ManyToManyField(
        ChemoType,
        through='ChemoRegime',
    )  
    def get_latest_chemo(self):
        chemo = ChemoRegime.objects.filter(patient=self).latest('stop_date')
        return chemo

    def get_latest_chemo_age(self):
        chemo = ChemoRegime.objects.filter(patient=self).latest('stop_date')
        return chemo.regime_age()

class ChemoRegime(models.Model):
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    chemotype = models.ForeignKey(ChemoType, on_delete=models.CASCADE)
    start_date = models.DateField(null=True, blank=True)
    stop_date = models.DateField(null=True, blank=True)

    def regime_age(self):
        age = timezone.now().date() - self.stop_date
        return age.days




In django_extension's shell plus:

>>> p = Patient.objects.get(id=1)
>>> p.get_latest_chemo()
<ChemoRegime: Gemcitabine>
>>> p.get_latest_chemo_age()
12
>>> p.get_latest_chemo_age
<bound method Patient.get_latest_chemo_age of <Patient: test-1>>
>>> type(p.get_latest_chemo_age())
<class 'int'>




In the template

          {% for patient in object_list %}
            <tr>
                   <td><a href="{{ patient.get_absolute_url }}">{{ patient }}</a></td>
                   <td>{{ patient.get_latest_chemo_age }} ago</td>
            </tr>




But in the browser:

Exception Type:     TypeError
Exception Value:    

unsupported operand type(s) for -: 'datetime.date' and 'NoneType'

Exception Location:     ./patients/models.py in regime_age, line 95


line 95 is         age = timezone.now().date() - self.stop_date


What am I doing wrong?

cheers
L.

James Schneider

unread,
Sep 1, 2017, 12:58:35 AM9/1/17
to django...@googlegroups.com


On Aug 31, 2017 9:44 PM, "Lachlan Musicman" <dat...@gmail.com> wrote:
Sorry, let me start again:


Yay, I'm not the only one that does it! Lol...

<snip>



class ChemoRegime(models.Model):
    patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    chemotype = models.ForeignKey(ChemoType, on_delete=models.CASCADE)
    start_date = models.DateField(null=True, blank=True)
    stop_date = models.DateField(null=True, blank=True)

    def regime_age(self):
        age = timezone.now().date() - self.stop_date
        return age.days

<snip>



But in the browser:

Exception Type:     TypeError
Exception Value:    

unsupported operand type(s) for -: 'datetime.date' and 'NoneType'

Exception Location:     ./patients/models.py in regime_age, line 95


line 95 is         age = timezone.now().date() - self.stop_date


What am I doing wrong?

cheers
L.

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

Lachlan Musicman

unread,
Sep 1, 2017, 1:23:14 AM9/1/17
to django...@googlegroups.com
On 1 September 2017 at 14:57, James Schneider <jrschn...@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.

James Schneider

unread,
Sep 1, 2017, 1:50:33 AM9/1/17
to django...@googlegroups.com



I am so embarrassed. Thank you James, spot on.

cheers
L.

No worries. I've stuck my foot in my mouth more than once on this list.

-James

Melvyn Sopacua

unread,
Sep 1, 2017, 2:13:51 AM9/1/17
to django...@googlegroups.com
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 :)
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@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

James Schneider

unread,
Sep 1, 2017, 2:38:09 AM9/1/17
to django...@googlegroups.com
Also, if you are developing this application for use within the USA, be sure that you are not capturing or storing data that would fall under the HIPAA definition of personal medical data. If it does, your legal compliance responsibilities and liabilities will skyrocket.

I'm not a lawyer, so I can't say for sure whether or not these data fields would be considered under HIPAA, but a compromise of medical data is extremely expensive, and I believe you can be held personally liable in some cases, even if developing for an employer.


If not in the US, there may be other regulations.

-James

Lachlan Musicman

unread,
Sep 1, 2017, 4:21:45 AM9/1/17
to django...@googlegroups.com
We are not in the US, but our laws are equally as strict. The culture isn't quite so litigious either.

Having said that, all "patients" will only have an id, and the id->name will be mapped on paper in a filing cabinet. On top of that, it's within the corporate firewall. Which isn't perfect, I'm sure, but it pretty good. You need to have access to the staff LAN (via eth) to access the site.

This is the first in a long "project to replace all access 2003/2007/2010 dbs" with something more...contemporary.

cheers
L.



 

Lachlan Musicman

unread,
Sep 4, 2017, 1:17:07 AM9/4/17
to django...@googlegroups.com
Melvyn - have only just got around to implementing your suggestions. Excellent, worked a treat. Thank you for the new tricks!

Cheers
Reply all
Reply to author
Forward
0 new messages