Django error in filtering datetime field by date : Join on field X not permitted
Question: I saw another post suggests that datetime field can be filtered by time, by
using `__date`. However when I tried on my machine it never worked.

This is my `models.py`

    
    
    class Record (models.Model):
        time = models.DateTimeField(null=True,blank=True)
        user = ForeignKey to the user table
        content = models.CharField(max_length=36,null=True,blank=True,unique=True)
    

In `python manage.py.runserver`

    
    
    >>> from datetime import datetime
    >>> from appname.models import Record
    
    >>> u = User.objects.filter(username = 'user')
    >>> r = Record.objects.filter(time__date = datetime.today().date())
    
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 163, in filter
        return self.get_queryset().filter(*args, **kwargs)
      File "C:\Python27\lib\site-packages\django\db\models\query.py", line 590, in filter
        return self._filter_or_exclude(False, *args, **kwargs)
      File "C:\Python27\lib\site-packages\django\db\models\query.py", line 608, in _filter_or_exclude
        clone.query.add_q(Q(*args, **kwargs))
      File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1198, in add_q
        clause = self._add_q(where_part, used_aliases)
      File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1234, in _add_q
        current_negated=current_negated)
      File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1100, in build_filter
        allow_explicit_fk=True)
      File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1357, in setup_joins
        names, opts, allow_many, allow_explicit_fk)
      File "C:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1320, in names_to_path
    
        "the lookup type?" % (name, names[pos + 1]))
    FieldError: Join on field 'timeIn' not permitted. Did you misspell 'date' for the lookup type?
    

I am on windows 7 running python 2.7, django 1.6

Any help is greatly appreciated!


Answer: Use __contains instead of __date:

    
    
    r = Record.objects.filter(time__contains = datetime.today().date())
    

**UPDATE**

Since that __startswith (LIKE 'value%') is more faster than __contains (LIKE
'%value%'), the best choice is:

    
    
    r = Record.objects.filter(time__startswith = datetime.today().date())
    

