Shiju
Shiju

Reputation: 71

'QuerySet' object has no attribute 'year' while using timesince

'QuerySet' object has no attribute 'year'
Request Method: GET
Request URL:    http://127.0.0.1:8000/dashboard/
Django Version: 2.2.8
Exception Type: AttributeError
Exception Value:    
'QuerySet' object has no attribute 'year'

models.py

joined_date = models.DateTimeField(default=now, editable=False)

views.py

date_joined = User.objects.order_by('-date_joined')

html

{{ date_joined|timesince }}

Upvotes: 2

Views: 144

Answers (2)

Pedram
Pedram

Reputation: 3920

date_joined is a queryset, not a single User object, so you can't use timesince template tag on it.

You should probably loop over the users, or alternatively, send a single User object to the template and use timesince on the DateTimeField, not the object itself:

{% for user in date_joined %}
    {{ user.joined_date|timesince }}
{% endfor %}

Upvotes: 2

Londrakan
Londrakan

Reputation: 1

You're probably making a mistake on the data type on your model. For instance, there is no such 'data type' as 'year.

You should check this website to data types for Django: https://www.geeksforgeeks.org/django-model-data-types-and-fields-list/

Also, If you share the relevant part of your code (possibly models.py), we can help you better. All the best!

Upvotes: 0

Related Questions