Search code examples
pythondjangomodels

Want to find date data in python shell


I have a model something like this.

#models.py
class ItemStatusHistory(models.Model):
    date = models.DateTimeField(auto_now = True)
    contact = models.ForeignKey(Contact)
    item = models.ForeignKey(StorageItem)
    status = models.ForeignKey(Status)
    user = models.ForeignKey(User)

    def __unicode__(self):
        return str(self.status)

I want to be able to find the date data using

python manage.py shell

But I want to keep the unicode object as status. Do I use a filter lookup?


Solution

  • Get the object from the django db in the usual way and access foo.name_of_attribute:

    The example in the django docs should help, see:

    http://docs.djangoproject.com/en/dev/intro/tutorial01/

     >>> p = Poll.objects.get(pk=1)
     >>> p.pub_date
     datetime.datetime(2007, 7, 15, 12, 00, 53)
    

    And thats a standard python datetime thing.