Use the package manager pip to install pgpython.
pip install pgpython
To connect to the database you must create .env file in your directory with needed variables. Example:
DB_NAME=testdb
DB_USER=c0nder
DB_HOST=localhost
DB_PASSWORD=yourpassword
DB_PORT=5432
And that's all you need to do!
Next we are going to deal with tables. Imagine that you have got a table called users with next columns:
- id (integer)
- username (text)
First, you have to import pgpython BaseModel to inherit your classes from it. Code:
from pgpython import BaseModel
Next, you must import fields that you will use in your code. Fields must match up with you column type. Example:
from pgpython.fields import IntegerField, StringField
There are 4 types in total: IntegerField, DateField, JSONField, StringField
Next, you must create a class called as your table name with attributes called as your columns. Code:
class users(BaseModel):
id = IntegerField()
username = StringField()
That's is all you need to tie up your class and table.
After creating a class you must create an object of this class. Using our example of users table let's code:
user = users()
Next, let's set our data to an object.
IMPORTANT If you have an auto_incremented id than you must not to set id to your object.
user.id = 1
user.username = 'c0nder'
Then, use method add()
user.add()
That's all!
At first, you must, as always, create an object and set data that you need to use to select data from table. Than use method select() to select data.
user = users()
user.username = 'c0nder'
data = user.selectBy({'username': user.username})
user = users.loadById(1)
user = users.loadById(1)
user.username = 'New_username'
user.update()
user = users.loadById(1)
user.delete()
user = users.loadById(1)
columns = user.getColumns()
for col, obj in columns.items():
print(col, ":", obj.value)
user = users.loadById(1)
print(user.username.value)
To work with schemas you need to import BaseSchema.
from pgpython import BaseModel, BaseSchema
Next, we will use our table users.
class users(BaseModel):
id = IntegerField()
username = StringField()
Than, you need to create schema class.
class schema_name(BaseSchema):
users = users()
Example of using:
user = schema_name.users.loadById(1)
That's all!
Added ReadMe