{"id":14065642,"url":"https://github.com/artofhuman/django-pg-upsert","last_synced_at":"2025-07-29T21:32:32.017Z","repository":{"id":40516344,"uuid":"224245814","full_name":"artofhuman/django-pg-upsert","owner":"artofhuman","description":"Support Postgres native upsert (INSERT ... ON CONFLICT) for django","archived":false,"fork":false,"pushed_at":"2023-03-11T16:41:52.000Z","size":94,"stargazers_count":31,"open_issues_count":3,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-04T05:39:31.926Z","etag":null,"topics":["django","hacktoberfest","postgresql","pyhton"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/artofhuman.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-26T17:11:31.000Z","updated_at":"2024-11-07T11:35:07.000Z","dependencies_parsed_at":"2024-05-28T01:36:56.750Z","dependency_job_id":null,"html_url":"https://github.com/artofhuman/django-pg-upsert","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/artofhuman/django-pg-upsert","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofhuman%2Fdjango-pg-upsert","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofhuman%2Fdjango-pg-upsert/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofhuman%2Fdjango-pg-upsert/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofhuman%2Fdjango-pg-upsert/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artofhuman","download_url":"https://codeload.github.com/artofhuman/django-pg-upsert/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artofhuman%2Fdjango-pg-upsert/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267763508,"owners_count":24140823,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-29T02:00:12.549Z","response_time":2574,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","hacktoberfest","postgresql","pyhton"],"created_at":"2024-08-13T07:04:36.570Z","updated_at":"2025-07-29T21:32:31.755Z","avatar_url":"https://github.com/artofhuman.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# django-pg-upsert\n\n![build](https://github.com/artofhuman/django-pg-upsert/workflows/build/badge.svg)\n[![PyPI version](https://badge.fury.io/py/django-pg-upsert.svg)](https://badge.fury.io/py/django-pg-upsert)\n[![Downloads](https://img.shields.io/pypi/dm/django-pg-upsert)](https://img.shields.io/pypi/dm/django-pg-upsert)\n\n\nSupport Postgres native upsert (INSERT ... ON CONFLICT) for django\n\n# Installation\n\n`pip install django-pg-upsert`\n\n# Usage\n\n## As a manager\n```python\n\nimport django_pg_upsert\n\nfrom django.db import models\n\nclass Pet(models.Model):\n    name = models.CharField(max_length=30, unique=True)\n    age = models.PositiveIntegerField()\n\n    objects = PgUpsertManager()\n\n\nPet.objects.insert_conflict(data={\"name\": \"dog\", \"age\": 12})\n\n# second query don't insert record to db and don't raise error\nPet.objects.insert_conflict(data={\"name\": \"dog\", \"age\": 20})\n\n# This code produce SQL statement\n\n[\n  'INSERT INTO \"pets\" (\"name\", \"age\") VALUES (%s, %s) ON CONFLICT DO NOTHING',\n  (\"dog\", 12),\n]\n```\n\n### Explicit constraint name\n\n``` python\nPet.objects.insert_conflict(\n  data={\"name\": \"dog\", \"age\": 12},\n  constraint=\"pet_name_uniq\"\n)\n\n[\n  'INSERT INTO \"pets\" (\"name\", \"age\") VALUES (%s, %s) ON CONSTRAINT pet_name_uniq DO NOTHING',\n  (\"dog\", 12),\n]\n\n```\n\n### Using field names\n\n\n``` python\nPet.objects.insert_conflict(\n  data={\"name\": \"dog\", \"age\": 12},\n  fields=[\"name\"]\n)\n\n[\n  'INSERT INTO \"pets\" (\"name\", \"age\") VALUES (%s, %s) ON CONSTRAINT (\"name\") DO NOTHING',\n  (\"dog\", 12),\n]\n\n```\n\n## As a standalone function\n\n```python\nimport django_pg_upsert\n\npet = Pet(name='dog', age='12')\n\ndjango_pg_upsert.insert_conflict(pet)\ndjango_pg_upsert.insert_conflict(pet, constraint='pet_name_uniq')\ndjango_pg_upsert.insert_conflict(pet, fields='name')\n```\n\n## Update\n\n``` python\nPet.objects.insert_conflict(\n  data={\"name\": \"dog\", \"age\": 100},\n  fields=[\"name\"],\n  update=[\"age\"]\n)\n```\nor\n\n```python\ndjango_pg_upsert.insert_conflict(pet, fields='name', update=[\"age\"])\n```\n\n```python\n[\n  'INSERT INTO \"pets\" (\"name\", \"age\") VALUES (%s, %s) ON CONFLICT (\"name\") DO UPDATE SET age = EXCLUDED.age',\n  (\"dog\", 100),\n]\n\n```\n\n# Motivation\n\n[django-postgres-extra](https://github.com/SectorLabs/django-postgres-extra) has\na pg upsert method as well, but this package requires redefinition for DB backed\nin Django settings which sometimes is not possible.\n\ndjango-pg-upsert is designed to solve only one problem (depends only from django) and is not a Swiss knife.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartofhuman%2Fdjango-pg-upsert","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartofhuman%2Fdjango-pg-upsert","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartofhuman%2Fdjango-pg-upsert/lists"}