Skip to main content

Model based ORM interface into Google Sheets, using the gspread package.

Project description

gspread-models-py

Model based ORM interface into Google Sheets, using the gspread package.

Installation

Install this package from PyPI:

pip install gspread_models

Setup

Follow the Google Cloud Setup Guide to setup a Google Cloud project with a service account access to the Google Sheets API, obtain a service account credentials JSON file.

Follow the Google Sheets Setup Guide to setup a Google Sheet document and share editor access with your service account.

Usage Example

Sheet Setup

To setup this example, create a sheet called "products", and populate the first row with the following column headers (including metadata columns):

  • id
  • name
  • description
  • price
  • url
  • created_at

The column names and order must match the COLUMNS defined in the model class (see below). The id column should be first, and the timestamps (created_at) should be last.

Model Class Definition

Define your own light-weight class that inherits from the base model:

from gspread_models.service import SpreadsheetService
from gspread_models.base import BaseModel

# bind the models to a specific document and set of credentials:
BaseModel.service = SpreadsheetService(
  document_id="your-document-id",
  credentials_filepath="/path/to/google-credentials.json"
)

# define your custom model class, which inherits from BaseModel:
class Product(BaseModel):

    # specify the name of the sheet to use for this model class:
    SHEET_NAME = "products"

    # specify the model-specific column names:
    COLUMNS = ["name", "description", "price", "url"]

In addition to the model-specific COLUMNS list, the base model will manage metadata columns, including:

  • an auto-incrementing integer (id), which acts as the record's unique identifier
  • auto-updating timestamps (created_at)

Query Interface

The base model provides an intuitive query interface.

Find All Records

Find all records from the sheet:

Product.all()

Find Record

Find a record given its unique identifier:

Product.find(1)

Filtering Records

Filter records based on class-specific attribute values (returns records that match ALL criteria):

Product.where(name="Strawberries")

Product.where(name="Strawberries", price=1000)

Creating Records

If the model class defines SEEDS as a list of dictionaries (see product.py example), you can populate the sheet with these initial values:

Product.seed()

Creating and persisting new records:

Product.create(dict(name="Blueberries", price=3.99, description="organic blues"))
Product.create_all([{"name":"Product X"}, {"name":"Product Y"}])

Destroying Records

Clear the sheet by removing all records:

Product.destroy_all()

Contributing

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gspread_models-1.0.3.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gspread_models-1.0.3-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file gspread_models-1.0.3.tar.gz.

File metadata

  • Download URL: gspread_models-1.0.3.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for gspread_models-1.0.3.tar.gz
Algorithm Hash digest
SHA256 c2d32c5cc8d828a09973f9f79b2a9f275595d0b557b7ff9aee875d21151341f9
MD5 9b51547302d326500a86497d3e840754
BLAKE2b-256 a945e27d39d6198202668e0f24d2e59dea64bb12fd7fc34a2a00f8d7f2c0ff2c

See more details on using hashes here.

File details

Details for the file gspread_models-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: gspread_models-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for gspread_models-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 23e7e07871285a323df1e271a317cbd3ff961c89d446109426991fd80b21601b
MD5 d8207808842023bcde63e362bbe66244
BLAKE2b-256 321ee27b78253d95d6e015398f0752fafed8a948c292461059f016d26319bd40

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page