Dealership with engine embedded in car document.

master
Michael Kennedy 8 years ago
parent 2bdbf9ef19
commit 6a4a0bb8b3

1
.gitignore vendored

@ -106,3 +106,4 @@ src/07_mongoengine/service_central/.idea/misc.xml
src/07_mongoengine/service_central/.idea/modules.xml
src/07_mongoengine/service_central/.idea/service_central.iml
src/07_mongoengine/service_central/.idea/inspectionProfiles/profiles_settings.xml
screencaster.xml

@ -0,0 +1,20 @@
import uuid
import mongoengine
from nosql.engine import Engine
class Car(mongoengine.Document):
model = mongoengine.StringField(required=True)
make = mongoengine.StringField(required=True)
year = mongoengine.IntField(required=True)
mileage = mongoengine.FloatField(default=0.0)
vi_number = mongoengine.StringField(
default=lambda: str(uuid.uuid4()).replace('-', ''))
engine = mongoengine.EmbeddedDocumentField(Engine, required=True)
meta = {
'db_alias': 'core',
'collection': 'cars',
}

@ -0,0 +1,11 @@
import uuid
import mongoengine
class Engine(mongoengine.EmbeddedDocument):
horsepower = mongoengine.IntField(required=True)
liters = mongoengine.FloatField(required=True)
mpg = mongoengine.FloatField(required=True)
serial_number = mongoengine.StringField(
default=lambda: str(uuid.uuid4())
)

@ -0,0 +1,5 @@
import mongoengine
def global_init():
mongoengine.register_connection(alias='core', name='demo_dealership')

@ -1,5 +1,11 @@
import nosql.mongo_setup as mongo_setup
from nosql.car import Car
from nosql.engine import Engine
def main():
print_header()
config_mongo()
user_loop()
@ -13,6 +19,10 @@ def print_header():
print()
def config_mongo():
mongo_setup.global_init()
def user_loop():
while True:
print("Available actions:")
@ -37,7 +47,23 @@ def user_loop():
def add_car():
print("TODO: add_car")
model = input("What is the model? ")
make = 'Ferrari' # input("What is the make? ")
year = int(input("Year built? "))
car = Car()
car.year = year
car.make = make
car.model = model
engine = Engine()
engine.horsepower = 590
engine.mpg = 22
engine.liters = 4.0
car.engine = engine
car.save()
def list_cars():

Loading…
Cancel
Save