librelist archives

« back to archive

Flask-MongoEngine - Building Models from Existing Data

Flask-MongoEngine - Building Models from Existing Data

From:
Nathan Farrar
Date:
2013-10-07 @ 00:21
I've got json data generated from another application that I'm attempting
to work with in a flask application (using Flask-MongoEngine).

I can see two approaches (I've been trying both of them, but neither are
working yet).

Approach 1:
Import the data using mongoimport (this works, but doesn't allow me to
customize the collection the way i'd like).
Now, I have to define models on this data.  The data set is large (50+
fields), and multiple fields are nested structures (some of which contain
more nested structures).  If I am going to take this approach, I'd like to
dynamically define the model from the data that's already in the database,
but I'm not sure exactly how this is possible.

Approach 2:
I am able to decode the json into nested python objects and then iteratate
through a list of these objects.

I was hoping that I could initialize a DynamicDocument model from a json
object, doing something like the following, but it isn't working either:

class Alert(db.DynamicDocument):
    meta = {'collection': 'alerts'}
    imported = db.DateTimeField(default=datetime.now)


def import_alerts(file_path=file_path):
    """ Import alerts from a json file """
    json_raw = open(file_path).read()
    json_data = demjson.decode(json_raw, strict=False)

    for alert in json_data['alert']:
        try:
            new_alert = Alert(alert)
            new_alert.save()
        except:
            pass

Any ideas/suggestions?

Re: [flask] Flask-MongoEngine - Building Models from Existing Data

From:
lynadge
Date:
2013-10-16 @ 05:38
Why do you want to put JSON in models? Is it necessary?

Am 2013-10-07 01:21, schrieb Nathan Farrar:
> I've got json data generated from another application that I'm
> attempting to work with in a flask application (using
> Flask-MongoEngine).
> 
> I can see two approaches (I've been trying both of them, but neither
> are working yet).
> 
> Approach 1:
> Import the data using mongoimport (this works, but doesn't allow me to
> customize the collection the way i'd like).
> Now, I have to define models on this data.  The data set is large
> (50+ fields), and multiple fields are nested structures (some of which
> contain more nested structures).  If I am going to take this
> approach, I'd like to dynamically define the model from the data
> that's already in the database, but I'm not sure exactly how this is
> possible.
> 
> Approach 2:
> I am able to decode the json into nested python objects and then
> iteratate through a list of these objects.
> 
> I was hoping that I could initialize a DynamicDocument model from a
> json object, doing something like the following, but it isn't working
> either:
> 
> class Alert(db.DynamicDocument):
>     meta = {'collection': 'alerts'}
>     imported = db.DateTimeField(default=datetime.now)
> 
> def import_alerts(file_path=file_path):
>     """ Import alerts from a json file """
>     json_raw = open(file_path).read()
>     json_data = demjson.decode(json_raw, strict=False)
> 
>     for alert in json_data['alert']:
>         try:
>             new_alert = Alert(alert)
>             new_alert.save()
>         except:
> 
>             pass
> 
> Any ideas/suggestions?