A Django library for providing useful DB model fields.
Project description
Fields of Gold
A Django library providing useful DB model fields.
Installation & setup
pip install fields-of-gold- Add
"fields_of_gold"to yourINSTALLED_APPS. - Use the fields in your models.
Fields
TypedJSONField
A JSONField which can have an enforced schema using Pydantic models. The underlying storage uses the JSONField, but you can interact with the data via the declared Pydantic type.
Because the underlying storage is using JSON, you can swap out existing JSONFields for TypedJSONField without having to perform any manipulation to the stored data, so long as the existing data conforms to your Pydantic schema.
Definition
TypedJSONField(
# The Pydantic model class of your data structure
type: type[pydantic.BaseModel],
# A JSON encoder class. The default uses `pydantic.BaseModel.model_dump(mode="json")`:
encoder: type[json.JSONEncoder] = PydanticJSONEncoder,
# Allows you to force validation of the data during save (rather than only during validation),
# raising IntegrityError for invalid data:
force_valid: bool = False,
# The other standard field kwargs, such as `null`, etc
**kwargs,
)
Example usage:
from django.db import models
from fields_of_gold import TypedJSONField
from pydantic import BaseModel
class MyType(BaseModel):
my_int: int
my_str: str
class MyModel(models.Model):
typed_field = TypedJSONField(type=MyType)
...
instance = MyModel(typed_field=MyType(my_int=1, my_str=2))
instance.full_clean()
instance.save()
Handling existing data
If you've already got data in your database which doesn't conform to the Pydantic model schema that
you've defined then when you fetch a Django model instance from the database the field value will
simply be set to the raw underlying JSON value rather than the Pydantic model instance.
This allows you to still fetch the objects from your DB, and to fix the data. Here an example
using the MyType and MyModel from above:
obj = MyModel.objects.get()
if not isinstnce(obj.typed_field, MyType):
# Invalid data, need to fix it:
if isinstance(obj.typed_field, dict):
if "my_int" not in obj.typed_field:
obj.typed_field["my_int"] = 0
if "my_str" not in obj.typed_field:
obj.typed_field["my_str"] = ""
# Note that leaving the data as a dict is fine, so long as it matches the schema of MyType
elif isinstance(obj.typed_field, list):
# Totally the wrong type of data
obj.typed_field = MyType()
obj.save()
You might want to handle existing invalid data in your model's __init__ method.
NullableOneToOneField
A modified OneToOneField which is unique but nullable.
In Django, using ForeignKey(unique=True, null=True) will raise a warning recommending that you should use
OneToOneField instead. But using OneToOneField(null=True) won't respect the null-able-ness, so trying to
access obj.my_one_to_one_field will raise DoesNotExist, and the same with the reverse lookup.
This field solves that problem by allowing the field to be nullable while still enforcing its uniqueness and not giving you unnecessary warnings.
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 fields_of_gold-0.1.3.tar.gz.
File metadata
- Download URL: fields_of_gold-0.1.3.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
232e76ebcb43f02be045b4c1cba38faa807a369c7e195a5f8aacb83d53cf5f5c
|
|
| MD5 |
d722306443d24f24e0dd05f40b48c283
|
|
| BLAKE2b-256 |
9fc57c4377d5e6f9c708b0167901751fe92fed3fda1db35c8e1f55dd17d5c5c8
|
File details
Details for the file fields_of_gold-0.1.3-py3-none-any.whl.
File metadata
- Download URL: fields_of_gold-0.1.3-py3-none-any.whl
- Upload date:
- Size: 8.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d71f74e85ec66fa0ce254a2b70302045cf3f498c5027f6c6a531527c383d764
|
|
| MD5 |
7828b0526e7c3b8ca678a95c45a03a77
|
|
| BLAKE2b-256 |
e6d1c0053d94c42e5213fb1b0f28123a0c2527d912f9e4854b7d5126d7848314
|