How to create a Django custom field with different representations in admin, database, and Python code?

I want to create a custom model field in Django that can store values in bytes in the database but allows users to interact with it in gigabytes (GB) in the Django admin interface. The goal is for the field to handle all necessary conversions seamlessly. Specifically, the field should accept input in GB when adding or editing a record in the admin, convert it to bytes before saving it to the database, and then retrieve it in bytes for use in Python code.

I’ve started working on this by overriding methods like to_python, get_prep_value, from_db_value, and formfield, but I’m not entirely sure how to structure these methods to ensure the field behaves as intended.

Here is what I already have:

class GigabyteField(models.BigIntegerField):
    def to_python(self, value):
        if value is None:
            return value
        try:
            return int(value)
        except (TypeError, ValueError):
            raise ValueError(f"Invalid value for GigabyteField: {value}")

    def get_prep_value(self, value):
        if value is None:
            return None
        return int(value * (1024**3))

    def from_db_value(self, value, *args):
        if value is None:
            return value
        return int(value / (1024**3))
Вернуться на верх