https://github.com/codeyellowbv/django-postgres-setfield
Django field plugin for sets, backed by array storage in PostgreSQL
https://github.com/codeyellowbv/django-postgres-setfield
Last synced: 8 months ago
JSON representation
Django field plugin for sets, backed by array storage in PostgreSQL
- Host: GitHub
- URL: https://github.com/codeyellowbv/django-postgres-setfield
- Owner: CodeYellowBV
- License: mit
- Created: 2019-11-20T08:23:39.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-09-16T02:48:17.000Z (almost 4 years ago)
- Last Synced: 2025-05-14T10:02:05.092Z (about 1 year ago)
- Language: Python
- Size: 15.6 KB
- Stars: 3
- Watchers: 4
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
django-postgres-setfield
========================
.. image:: https://travis-ci.org/CodeYellowBV/django-postgres-setfield.svg?branch=master
:target: https://travis-ci.org/CodeYellowBV/django-postgres-setfield
A Django field for storing `standard Python set `_
objects. It uses Postgres arrays as a backing store.
Usage
-----
Using the field is straightforward, and similar to how you'd use a
`Django ArrayField `_.
You can add the field to your model like so:
.. code:: python
from django.db import models
from setfield import SetField
class Person(models.Model):
LANGUAGES = (('NL', 'Dutch'), ('EN', 'English'), ('RU', 'Russian'))
speaks_languages=SetField(models.TextField(choices=LANGUAGES), default=list, blank=True)
Then later, you can use it:
.. code:: python
piet = Person(languages={'NL'})
piet.save()
john = Person(languages={'RU', 'EN'})
john.save()
Lookups
-------
All the standard `Django ArrayField`_ lookups are supported.
Caveats
-------
* Unlike ArrayFields, SetFields cannot be nested (because sets cannot
be nested in Python).
* When upgrading an existing ArrayField to a SetField, make sure the
entries are sorted using the default sort order of Python for the
corresponding object type, if you want to use the ``__exact``
lookup. Otherwise you'll get inconsistent results.