title: Trying to delete all objects older than 7 days in django and I keep getting TypeError: unorderable types: DeferredAttribute() < datetime.datetime()
body: <p>I am trying to create a web page that displays a status that is uploaded to a MySQL server from a sonic sensor. </p>
  
  <p>I've tried many different arrangements of the selections but none seem to work. </p>
  
  <p>When I created my first Django project I followed the tutorial and their example, selecting objects created in the past day seemed to work. </p>
  
  <pre><code>return self.pub_date &gt;= timezone.now() - datetime.timedelta(days=1)
  </code></pre>
  
  <p>In my project, I just changed days to 7 and I changed around >= to be &lt;. Then I started getting "TypeError: unorderable types: DeferredAttribute() &lt; datetime.datetime()".</p>
  
  <p>My models.py:</p>
  
  <pre><code>from django.db import models
  import datetime
  from django.utils import timezone
  
  class garage_door(models.Model):
      date = models.DateTimeField(auto_now_add=True)
      state = models.CharField(max_length=6)
  
      def __str__(self):
          return str(self.date)[:19]
  
      def select_old(self):
          return self.date &lt; timezone.now() - datetime.timedelta(days=7)
  </code></pre>
  
  <p>My views.py where I am calling this function:</p>
  
  <pre><code>from django.shortcuts import render
  from django.http import HttpResponse
  
  from dash.models import garage_door
  
  def dashboard(request):
      garage_door.select_old(garage_door).delete()
  
      return HttpResponese("Temp Response")
  </code></pre>
  
  <p>I want the program to (as stated above) delete all objects older than 7 days, but for some reason, I get this error. What I got from it is that I am comparing two uncomparable variables, but I don't know how to fix that and I don't even know if my guess about this is correct. </p>
  
  <p>Can it have something to do with the date being added into the table from a different raspberry like this:</p>
  
  <pre><code>sql = "INSERT INTO dash_garage_door(date, state) VALUES(%s, %s)"
  now = datetime.datetime.now()
  val = (now, g_state)
  mycursor.execute(sql, val)
  
  mydb.commit()
  </code></pre>
  
  <p>(The state is either "open" or "closed")</p>
  
  <p>I would be very thankful for any help ! If there is something needed to solve this problem that I haven't provided I would be happy to provide it.</p>