Thursday, August 31, 2017

Re: Weird date math bug?

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.

------
"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+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/CAGBeqiMoZXiwUhL-gL1uPzOw%3DC4JfoDZnfOFs6Qys4HU7RZV%2BQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment