Skip to main content

Django ORM connector for Google BigQuery

Project description

Django BigQuery Connector

A Django database backend for Google BigQuery, enabling Django ORM queries against BigQuery datasets.

Features

  • Use Django ORM to query BigQuery tables seamlessly
  • Read-only operations support
  • Supports most Django ORM query methods and filters
  • GIS operations support (minimal)
  • Django admin integration

Installation

pip install django-bigquery-connector

Configuration

Add the following to your Django settings:

DATABASES = {
    'default': {
        # Your regular database for models, auth, etc.
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    },
    'bigquery': {
        'ENGINE': 'django_bq',
        'PROJECT': 'your-gcp-project-id',
        'CREDENTIALS': None,  # Uses default credentials, or provide a credentials object
        'LOCATION': 'us-central1',  # Optional: BigQuery location
        'NAME': 'your-bigquery-dataset-name'
    }
}

Authentication

You can provide credentials in three ways:

  1. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account JSON file:

    export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account-file.json"
    
  2. Pass a credentials object in the settings:

    from google.oauth2 import service_account
    credentials = service_account.Credentials.from_service_account_file(
        '/path/to/your/service-account-file.json',
    )
    
    DATABASES = {
        'bigquery': {
            'ENGINE': 'django_bq',
            'PROJECT': 'your-gcp-project-id',
            'CREDENTIALS': credentials,
            'NAME': 'your-bigquery-dataset-name'
        }
    }
    
  3. Use Application Default Credentials (ADC) by setting CREDENTIALS to None.

Usage Examples

Direct SQL Queries

from django.db import connections

# Using the BigQuery connection
with connections['bigquery'].cursor() as cursor:
    cursor.execute("SELECT * FROM `your-project.dataset.table` LIMIT 10")
    results = cursor.fetchall()
    for row in results:
        print(row)

Using Django Models

  1. Define models with BigQuery tables:
from django.db import models

class Customer(models.Model):
    id = models.IntegerField(primary_key=True)
    name = models.CharField(max_length=100)
    email = models.EmailField()

    class Meta:
        managed = False  # Important: we don't want Django to create/modify tables
        db_table = 'customers'  # Your BigQuery table name
  1. Query using Django ORM:
# Get all records
customers = Customer.objects.using('bigquery').all()

# Filter records
active_customers = Customer.objects.using('bigquery').filter(
    status='active',
    registration_date__gte='2023-01-01'
)

# Aggregate functions
from django.db.models import Count, Avg
customer_stats = Customer.objects.using('bigquery').values('country').annotate(
    count=Count('id'),
    avg_purchases=Avg('purchase_count')
)

Limitations

  • Read-only operations only (no INSERT, UPDATE, DELETE)
  • Regular expression operations need improvements
  • Some Django ORM features may not be fully supported
  • Complex JOIN operations may not work as expected

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

License

MIT License - See LICENSE file for details.

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

django_bigquery_connector-0.1.3.tar.gz (12.3 kB view details)

Uploaded Source

Built Distribution

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

django_bigquery_connector-0.1.3-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file django_bigquery_connector-0.1.3.tar.gz.

File metadata

File hashes

Hashes for django_bigquery_connector-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4b2875e903a234dfb5bc4fd77a0d2293437c5b30e80b0ddfd40b00d413a20959
MD5 606ab4cd24b916890bd1e61c00d97d7c
BLAKE2b-256 15910489da502c3bd51ea57afba030c8a031d3e348ce5c38bdcde878be225ad6

See more details on using hashes here.

File details

Details for the file django_bigquery_connector-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_bigquery_connector-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 30c817cc27c2348aa1a4d49b5f49600b2febe59a135ea8052f74ca11b54c6a1a
MD5 adea0dd59e2ca8e46233cc4fad229f8d
BLAKE2b-256 94fa1378ba093dbbadf8cb0516b0e1cdf7d35fe3f1ccc6c787026e1f67982785

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