Python database WITHOUT using Django (for Heroku)

前端 未结 4 462
误落风尘
误落风尘 2021-02-02 15:54

To my surprise, I haven\'t found this question asked elsewhere. Short version, I\'m writing an app that I plan to deploy to the cloud (probably using Heroku), which will do var

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-02 16:30

    I'd use MongoDB. Heroku has support for it, so I think it will be really easy to start and scale out: https://addons.heroku.com/mongohq

    About Python: MongoDB is a really easy database. The schema is flexible and fits really well with Python dictionaries. That's something really good.

    You can use PyMongo

    from pymongo import Connection
    connection = Connection()
    
    # Get your DB
    db = connection.my_database
    
    # Get your collection
    cars = db.cars
    
    # Create some objects
    import datetime
    car = {"brand": "Ford",
           "model": "Mustang",
           "date": datetime.datetime.utcnow()}
    
    # Insert it
    cars.insert(car)
    

    Pretty simple, uh?

    Hope it helps.

    EDIT:

    As Endophage mentioned, another good option for interfacing with Mongo is mongoengine. If you have lots of data to store, you should take a look at that.

提交回复
热议问题