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
}
}
# Optional: Set a default BigQuery dataset for all queries
BIGQUERY_DATASET = 'your_dataset_name'
Authentication
You can provide credentials in three ways:
-
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"
-
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, } }
-
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
- 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
- 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_bigquery_connector-0.1.1.tar.gz.
File metadata
- Download URL: django_bigquery_connector-0.1.1.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06067b245347b2f16fb1f24c1bad577935724c764d190e3c674c41ee04e94f51
|
|
| MD5 |
96f70d1375a672f4e76a2e6ef1b74ae3
|
|
| BLAKE2b-256 |
81e3ab513783100028b931628c9e958d84bac1ff1072a4921678456ee9bfa19a
|
File details
Details for the file django_bigquery_connector-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_bigquery_connector-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
49b82194cfa1f389c2142e28652e54609063ba1c968e6d5121e84b6990a0c804
|
|
| MD5 |
f1938c672fd8ec585d44f527b13ffbf1
|
|
| BLAKE2b-256 |
94b3d963787915ca0d938dc67be6fccdec0766f2a3ae3525d7d9b9d2a50e2fba
|