nycynik
nycynik

Reputation: 7541

Fix "unsupported date format" when marshaling data from rest endpoint

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.

Upvotes: 2

Views: 2016

Answers (1)

Aleksandr Kovalev
Aleksandr Kovalev

Reputation: 3734

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()

Upvotes: 2

Related Questions