0
print SDstDate

gives below output:

<peewee.DateTimeField object at 0x7fd9dc8b0950>

and

print SDstDate.year

gives below output:

<peewee.Func object at 0x7f3737b0ef10>

Please help how can i extract the extract year from SDstDate. SDstDate is a dattetimefield object which i have fetched from mysql database through peewee.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Afroz Alam
  • 874
  • 6
  • 19
  • @Ajay i get error: TypeError: 'Func' object is not callable when use print SDstDate.year() – Afroz Alam Jun 09 '15 at 12:18
  • can you post your code – Ajay Jun 09 '15 at 12:18
  • DateTimeField objects are used to query tables. E.g. `foo.select().where(foo.SDstDate.year == 2015)` – Ogen Jun 09 '15 at 12:21
  • @Ogen - I think you have understood my question well. CAn you please tell me how can i print that year instead of comparing. – Afroz Alam Jun 09 '15 at 12:25
  • @AfrozAlam That's the thing. This datetimefield object does not correspond to an entry. You have to use it to obtain desired entries in your table. – Ogen Jun 09 '15 at 12:47

1 Answers1

2

So let's say you have a model with a DateTimeField indicating when a blog entry is published:

class Blog(Model):
    timestamp = DateTimeField()

To retrieve the year from a list of posts, simply do:

for post in Blog.select():
    print post.timestamp.year

Because the value in the timestamp field is a Python datetime, you can access properties on it like year, month, etc.

On the other hand, to query for blog entries published in a given year, you can write:

posts_this_year = Blog.select().where(Blog.timestamp.year == 2015)
for post in posts_this_year:
    # ... do whatever ...
coleifer
  • 24,887
  • 6
  • 60
  • 75