2

I am trying to figure out why the date is not working in flask-restplus.

MarshallingError: Unable to marshal field "lastUpdate" value "<built-in method now of type object at 0x10264d6e0>": Unsupported DateTime format
127.0.0.1 - - [16/Apr/2016 22:24:18] "POST /api/v1/course/record/ab HTTP/1.1" 500 -

And here is the object that is used for marshaling

course_record_model = ns.model('Model', {
  'topic': fields.String,
  'totalMinutes': fields.Integer,
  'percentComplete': fields.Integer,
  'lastUpdate': fields.DateTime,
})

Note the fields.DateTime. That is the one with the issue.

def __init__(self, courseid, memberid, **kwargs):
    """Create instance."""
    db.Model.__init__(self, **kwargs)
    self.courseID = courseid
    self.memberID = memberid
    self.lastUpdate = datetime.datetime.now

I have tried adding some formats, but it does not seem to help, here are the docs

class fields.DateTime(dt_format='rfc822', **kwargs) Return a formatted datetime string in UTC. Supported formats are RFC 822 and ISO 8601.

See email.utils.formatdate() for more info on the RFC 822 format.

See datetime.datetime.isoformat() for more info on the ISO 8601 format.

Parameters: dt_format (str) – 'rfc822' or 'iso8601'

Not sure how to make the date format when it is coming in from the API call.

nycynik
  • 7,371
  • 8
  • 62
  • 87

1 Answers1

2

As you can see, you have "<built-in method now of type object at 0x10264d6e0>" instead of datetime object.

I suspect that somewhere in your code you forgot to type parenthesis () like this:

someobject.lastUpdate = datetime.now

But it should be

someobject.lastUpdate = datetime.now()
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
Aleksandr Kovalev
  • 3,508
  • 4
  • 34
  • 36
  • That was it, now returning "lastUpdate": "2016-04-17T16:03:43.525476" – nycynik Apr 17 '16 at 20:04
  • self.lastUpdate = datetime.datetime.now was not self.lastUpdate = datetime.datetime.now() – nycynik Apr 17 '16 at 20:05
  • 2
    @nycynik: unrelated: don't use `datetime.now()`, use either `datetime.utcnow()` or `datetime.now(some_timezone)` instead. Mere `datetime.now()` may produce an ambiguous value and the utc offset may be different at different times (or in different time zones). – jfs Apr 18 '16 at 09:35
  • I was following the documentation here: http://docs.sqlalchemy.org/en/latest/core/defaults.html - Is it true that it is totally wrong? it shows # define 'last_updated' to be populated with datetime.now() Column('last_updated', DateTime, onupdate=datetime.datetime.now), Thanks for your help, I am now using (), and utcnow() – nycynik Apr 18 '16 at 14:41
  • Nevermind. I got it, when you add it to a constructor, it should not have the (), since it is a function ref that is called whenever the event happens. If you call with the () then it will have the same date for all events. If you call it in the constructor however, you need to use the actual call (). – nycynik Apr 18 '16 at 21:16