Peewee Tutorial
Selected Reading
- Peewee - Discussion
- Peewee - Useful Resources
- Peewee - Quick Guide
- Peewee - Using CockroachDB
- Peewee - PostgreSQL & MySQL Extensions
- Peewee - SQLite Extensions
- Peewee - Integration with Web Frameworks
- Peewee - Query Builder
- Peewee - Database Errors
- Peewee - Atomic Transactions
- Peewee - User defined Operators
- Peewee - Retrieving Row Tuples/Dictionaries
- Peewee - SQL Functions
- Peewee - Counting & Aggregation
- Peewee - Sorting
- Peewee - Subqueries
- Peewee - Relationships & Joins
- Peewee - Connection Management
- Peewee - Defining Database Dynamically
- Peewee - Using PostgreSQL
- Peewee - Using MySQL
- Peewee - Constraints
- Peewee - Create Index
- Peewee - Delete Records
- Peewee - Update Existing Records
- Peewee - Primary & Composite Keys
- Peewee - Filters
- Peewee - Select Records
- Peewee - Insert a New Record
- Peewee - Field Class
- Peewee - Model
- Peewee - Database Class
- Peewee - Overview
- Peewee - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Peewee - Defining Database Dynamically
Peewee - Defining Database Dynamically
If your database is scheduled to vary at run-time, use DatabaseProxy helper to have better control over how you initiapse it. The DatabaseProxy object is a placeholder with the help of which database can be selected in run-time.
In the following example, an appropriate database is selected depending on the apppcation’s configuration setting.
from peewee import * db_proxy = DatabaseProxy() # Create a proxy for our db. class MyUser (Model): name=TextField() city=TextField(constraints=[SQL("DEFAULT Mumbai ")]) age=IntegerField() class Meta: database=db_proxy db_table= MyUser # Based on configuration, use a different database. if app.config[ TESTING ]: db = SqpteDatabase( :memory: ) epf app.config[ DEBUG ]: db = SqpteDatabase( mydatabase.db ) else: db = PostgresqlDatabase( mydatabase , host= localhost , port=5432, user= postgres , password= postgres ) # Configure our proxy to use the db we specified in config. db_proxy.initiapze(db) db.connect() db.create_tables([MyUser])
You can also associate models to any database object during run-time using bind() method declared in both database class and model class.
Following example uses bind() method in database class.
from peewee import * class MyUser (Model): name=TextField() city=TextField(constraints=[SQL("DEFAULT Mumbai ")]) age=IntegerField() db = MySQLDatabase( mydatabase , host= localhost , port=3306, user= root , password= ) db.connect() db.bind([MyUser]) db.create_tables([MyUser])
The same bind() method is also defined in Model class.
from peewee import * class MyUser (Model): name=TextField() city=TextField(constraints=[SQL("DEFAULT Mumbai ")]) age=IntegerField() db = MySQLDatabase( mydatabase , host= localhost , port=3306, user= root , password= ) db.connect() MyUser.bind(db) db.create_tables([MyUser])Advertisements