1

I try show post only from 30 days. What I do worng?

@login_required
def dashboard(request):
    days = 30
    posts = Post.objects.filter(Post.publish < timezone.now() - timedelta(days=days))
    #posts = Post.objects.all()
    return render(request, 'account/dashboard.html', {'section': 'dashboard', 'posts': posts})

error

TypeError at /account/
unorderable types: DeferredAttribute() < datetime.datetime()
1
  • You could have solved the problem by yourself reading the documentation... Commented Jun 12, 2018 at 9:07

1 Answer 1

4

Intead of < sign inside filter method you should use __lt lookup attached to field name:

posts = Post.objects.filter(publish__lt=timezone.now() - timedelta(days=days))
Sign up to request clarification or add additional context in comments.

2 Comments

FieldError at /account/ Cannot resolve keyword 'publish_lt' into field. Choices are: author, author_id, body, id, location, publish, title
@JestemPokrowcem Double underscore, not single. That's how you access these methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.