Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/artofhuman/django-pg-upsert
Support Postgres native upsert (INSERT ... ON CONFLICT) for django
https://github.com/artofhuman/django-pg-upsert
django hacktoberfest postgresql pyhton
Last synced: 3 months ago
JSON representation
Support Postgres native upsert (INSERT ... ON CONFLICT) for django
- Host: GitHub
- URL: https://github.com/artofhuman/django-pg-upsert
- Owner: artofhuman
- License: mit
- Created: 2019-11-26T17:11:31.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-11T16:41:52.000Z (over 1 year ago)
- Last Synced: 2024-05-28T04:55:25.372Z (5 months ago)
- Topics: django, hacktoberfest, postgresql, pyhton
- Language: Python
- Homepage:
- Size: 91.8 KB
- Stars: 26
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - artofhuman/django-pg-upsert - Support Postgres native upsert (INSERT ... ON CONFLICT) for django (Python)
README
# django-pg-upsert
![build](https://github.com/artofhuman/django-pg-upsert/workflows/build/badge.svg)
[![PyPI version](https://badge.fury.io/py/django-pg-upsert.svg)](https://badge.fury.io/py/django-pg-upsert)
[![Downloads](https://img.shields.io/pypi/dm/django-pg-upsert)](https://img.shields.io/pypi/dm/django-pg-upsert)Support Postgres native upsert (INSERT ... ON CONFLICT) for django
# Installation
`pip install django-pg-upsert`
# Usage
## As a manager
```pythonimport django_pg_upsert
from django.db import models
class Pet(models.Model):
name = models.CharField(max_length=30, unique=True)
age = models.PositiveIntegerField()objects = PgUpsertManager()
Pet.objects.insert_conflict(data={"name": "dog", "age": 12})
# second query don't insert record to db and don't raise error
Pet.objects.insert_conflict(data={"name": "dog", "age": 20})# This code produce SQL statement
[
'INSERT INTO "pets" ("name", "age") VALUES (%s, %s) ON CONFLICT DO NOTHING',
("dog", 12),
]
```### Explicit constraint name
``` python
Pet.objects.insert_conflict(
data={"name": "dog", "age": 12},
constraint="pet_name_uniq"
)[
'INSERT INTO "pets" ("name", "age") VALUES (%s, %s) ON CONSTRAINT pet_name_uniq DO NOTHING',
("dog", 12),
]```
### Using field names
``` python
Pet.objects.insert_conflict(
data={"name": "dog", "age": 12},
fields=["name"]
)[
'INSERT INTO "pets" ("name", "age") VALUES (%s, %s) ON CONSTRAINT ("name") DO NOTHING',
("dog", 12),
]```
## As a standalone function
```python
import django_pg_upsertpet = Pet(name='dog', age='12')
django_pg_upsert.insert_conflict(pet)
django_pg_upsert.insert_conflict(pet, constraint='pet_name_uniq')
django_pg_upsert.insert_conflict(pet, fields='name')
```## Update
``` python
Pet.objects.insert_conflict(
data={"name": "dog", "age": 100},
fields=["name"],
update=["age"]
)
```
or```python
django_pg_upsert.insert_conflict(pet, fields='name', update=["age"])
``````python
[
'INSERT INTO "pets" ("name", "age") VALUES (%s, %s) ON CONFLICT ("name") DO UPDATE SET age = EXCLUDED.age',
("dog", 100),
]```
# Motivation
[django-postgres-extra](https://github.com/SectorLabs/django-postgres-extra) has
a pg upsert method as well, but this package requires redefinition for DB backed
in Django settings which sometimes is not possible.django-pg-upsert is designed to solve only one problem (depends only from django) and is not a Swiss knife.