python - Django: Mongoengine equivalent $addtoset -


this models.py:

from mongoengine import *  class venue(document):     location_id     =   stringfield(required=true)     name            =   stringfield(required=true)     latitude        =   floatfield(required=true)     longitude       =   floatfield(required=true)     address         =   stringfield()     postal_code     =   stringfield()     city            =   stringfield()     county          =   stringfield()     country_code    =   stringfield()     events          =   listfield()      class event(document):         title           =   stringfield(required=true)         description     =   stringfield(required=true)         website         =   stringfield()         start_date      =   datetimefield(required=true)         start_time      =   datetimefield(required=true)         end_date        =   datetimefield(required=true)         end_time        =   datetimefield(required=true) 

clearly each venue has many event.

how can push event model mongodb @ end of list events of venue model assuming target based on location_id field?

for example in pymongo use:

db.venue.update({ 'location_id': id },         { '$addtoset' :{ 'events' : {   'title': title,                                         'website' : website,                                         'description' : description,                                         'start_date' : start_date,                                         'start_time' : start_time,                                         'end_date' : end_date,                                         'end_time' : end_time                                     }                            }     }) 

if have more optimal structure please feel free suggest so.

maybe using referencefield inside listfield

class venue(document):     location_id = stringfield(required=true)     name = stringfield(required=true)     latitude = floatfield(required=true)     longitude = floatfield(required=true)     address = stringfield()     postal_code = stringfield()     city = stringfield()     county = stringfield()     country_code = stringfield()     events = listfield(referencefield('event'), default=[])  class event(document):     title = stringfield(required=true)     description = stringfield(required=true)     website = stringfield()     start_date = datetimefield(required=true)     start_time = datetimefield(required=true)     end_date = datetimefield(required=true)     end_time = datetimefield(required=true) 

and venue().events.append(event()) shall help?

p.s. please not use multiple spaces around =


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -