strptime() argument 1 must be str, not DeferredAttribute

Viewed 32

I am heaving trouble here, am making a travel and visa website, am trying to make sure we know each appointment 10 days in advance. So I made a model property that would help me compare dates, but am having trouble error after error here is the model field

class customer(models.Model):
first_name = models.CharField(max_length=255, default='')
last_name = models.CharField(max_length=255, default='')
biometry_date = models.DateField(blank=True, default='', null=True)


@property
def is_due(self):
    datetime_object = datetime.strptime(Profile.biometry_date, '%Y-%m-%d')
    t2 = datetime.now().date()
    t3 = timedelta(days=30)
    return  datetime_object - t2<= t3

and in the Html template

{% if customer.is_due %}
<td class="text-right">
<span class="badge badge-danger">{{ profile.biometry_date }}</span>
</td>
{% else %}
<td>{{ customer.biometry_date }}</td>
{% endif %}

Please help me Thanks

1 Answers

Firstfully, as Willem pointed - you don't need to use strptime from field, it's already datetime.datetime object. And you should use self when you want to use the object's field values. So:

def is_due(self):
    datetime_object = self.biometry_date

Secondly, I think you try to use interchangeably profile and customer. Decide which you are going to use or at least share the context.

Related