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.2.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: gspread_models-1.0.2.tar.gz
  • Upload date:
  • Size: 6.6 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.2.tar.gz
Algorithm Hash digest
SHA256 4f9c7d204bbfae1aa21148c3660a44875974cf2852a10ad08e994137486ea1f0
MD5 9f2b09860206f47f221dd790e8adb304
BLAKE2b-256 b4d35d666853501d3eced5d2e51984723f8428926bcd23676ab630a477c94d0b

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for gspread_models-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0ffba7e8009dec5bab8ec8b75f99c2368494081ac70461d67c397cba750042c6
MD5 02d46159d70570d313f6d04f8148a2e7
BLAKE2b-256 8507775d1bb02ea2e4e6531c74e6b33fdd371aa04c4b5d063b03df9fee000cec

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