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.base import BaseModel

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

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

    # the model-specific columns (excluding metadata columns):
    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.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

gspread_models-1.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gspread_models-1.0.tar.gz
  • Upload date:
  • Size: 6.1 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.tar.gz
Algorithm Hash digest
SHA256 66515763c7d97ac68cbffbe62707ce5f3bc5657f310b5adb370d131519247eed
MD5 9f39d00618b505579bdbc659ee87eb76
BLAKE2b-256 64213766ff21a1542d379b8713d2e2516db561fac1b23c89d0345a63c05fcb16

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for gspread_models-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e647846ace311b1bed9d29e314a81435c8e95c010c2371e9e417c1817a66766
MD5 0a7397aee4bed919877b97af3c59de96
BLAKE2b-256 9dd499c978d3a902268f988a547886b5830787c10168549c22b012c82414a4ac

See more details on using hashes here.

Provenance

Supported by

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