def mock_field_default(field): from django.db import DefaultConnectionProxy if field.has_default(): if callable(field.default): if field.default.__name__ == 'now': return datetime.datetime.now() return field.default() return field.default if not field.empty_strings_allowed or \ (field.null and not DefaultConnectionProxy().features.interprets_empty_strings_as_nulls): return None return ''
from django.db import models from django.utils.translation import gettext_lazy as _ import ethereum.utils from hexbytes import HexBytes from .validators import validate_checksumed_address try: from django.db import DefaultConnectionProxy connection = DefaultConnectionProxy() except ImportError: from django.db import connections connection = connections['default'] class EthereumAddressField(models.CharField): default_validators = [validate_checksumed_address] description = "Ethereum address" def __init__(self, *args, **kwargs): kwargs['max_length'] = 42 super().__init__(*args, **kwargs) def deconstruct(self): name, path, args, kwargs = super().deconstruct() del kwargs['max_length'] return name, path, args, kwargs def from_db_value(self, value, expression, connection): return self.to_python(value)