John
John

Reputation: 1

How to define a DateProperty object in App Engine

I am facing difficulties in creating dateproperty object and I can't seems to create the object. Here is my code:

from google.appengine.ext import db

class Baby(db.Model):
    name = db.StringProperty()
    dob = db.DateProperty()

for i in Baby.all():
    delete(i)

Baby(name='wilson', dob=Date(year=1986,month=3,day=5)).put()

Is there anything wrong with my code?

Thanks for the help.

Upvotes: 0

Views: 1347

Answers (2)

Abdul Kader
Abdul Kader

Reputation: 5842

As @Elliot said, db.DateProperty() accepts only datetime.date object.To know more about datetime you can see here.This datetime.date propery gives you much more flexibiliy and opeartions. And also using timedelta you can do much more manipulation like adding one day ahead and so. so you have to import date time as

from datetime import date
year=self.request.get['year']
month=self.request.get['month']
day=self.request.get['day']
dob=date(int(year),int(month),int(day))
baby=Baby()
baby.name='wilson'
baby.date=dob
baby.put()

Upvotes: 0

user458962
user458962

Reputation:

The value of a DateProperty will need to be a datetime.date object, as mentioned here: http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#DateProperty

So, you will need to add:

import datetime

and also change dob=Date(year=1986, month=3, day=5) to:

dob = datetime.date(year=1986, month=3, day=5)

Upvotes: 5

Related Questions