Skip to main content

Fields of Gold

A Django library providing useful DB model fields.

Installation & setup

  1. pip install fields-of-gold
  2. Add "fields_of_gold" to your INSTALLED_APPS.
  3. 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.

Metadata

Release files for fields-of-gold 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fields-of-gold 0.1.3
File Size Uploaded Provenance
fields_of_gold-0.1.3.tar.gz 8.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for fields-of-gold 0.1.3
File Interpreter ABI Platform
fields_of_gold-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / fields_of_gold-0.1.3.tar.gz

Download URL fields_of_gold-0.1.3.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
232e76ebcb43f02be045b4c1cba38faa807a369c7e195a5f8aacb83d53cf5f5c
BLAKE2b-256 checksum
How to use checksums
9fc57c4377d5e6f9c708b0167901751fe92fed3fda1db35c8e1f55dd17d5c5c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / fields_of_gold-0.1.3-py3-none-any.whl

Download URL fields_of_gold-0.1.3-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d71f74e85ec66fa0ce254a2b70302045cf3f498c5027f6c6a531527c383d764
BLAKE2b-256 checksum
How to use checksums
e6d1c0053d94c42e5213fb1b0f28123a0c2527d912f9e4854b7d5126d7848314
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for fields-of-gold 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page