How can I let BinaryField edit by hex str?

19 views
Skip to first unread message

ivory 954

unread,
Jan 4, 2022, 9:36:22 AM1/4/22
to Django users
I rewrited SelfModel.save() to do that.
But I feel it wasn't a good way.

So , I tried to create a CustomField to do this , but It looks like some wrong.
It can't render a hex str in my url , how can I fix it ?
should I rewrite get_db_prep_value() or get_lookups() ?

class BinaryHexField(BinaryField):
    description = _("Raw binary data to hex str")

    def __init__(self, *args, **kwargs):
    kwargs['editable'] = True
    super().__init__(*args, **kwargs)

    def value_to_string(self, obj):
        value = self.value_from_object(obj)
        return value.hex(value)

    def to_python(self, value):
        if isinstance(value, str):
            return bytes.fromhex(value)
            return value

Reply all
Reply to author
Forward
0 new messages