Tuesday, January 4, 2022

How can I let BinaryField edit by hex str?

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/36293a79-5f17-4ed2-9c71-c2555118859dn%40googlegroups.com.

No comments:

Post a Comment