0

I'm trying to implement custom field in my model following the docs, My model is as follow:

class User(AbstractUser):
    uid = models.CharField(
        "uid", max_length=255, null=True, blank=True)
    phone_number = models.CharField(
        "Phone number", max_length=255, null=True, blank=True)
    nickname = models.CharField(
        "Nickname", max_length=255, null=True, blank=True)

    eth_address = models.CharField("Eth address", max_length=255, null=True, blank=True)

    created = models.DateTimeField(auto_now_add=True)
    modified = models.DateTimeField(auto_now=True)

    # deleted
    class eth_address_decrypted(models.Field):
        description = 'eth_address_decrypted'

        def __init__(self, *args, **kwargs):
            return decrypt_string(self.eth_address)


    class Meta:
        db_table = "users"

    pass

I have a eth_address field that it's value are encrypted, i need to display the decrypted value on the frontend, so i wrote custom field model as the docs and call it on my front end through query:

User.objects.get(eth_address_decrypted=value)

But it returning the following error:

Cannot resolve keyword 'eth_address_decrypted' into field. Choices are: created, date_joined, email, eth_address,

What am i doing wrong ?

Is there a way i can call user.eth_address_decrypted on object as a custom field or function without migrating it ? (because eth_address_decrypted just a convert from existed eth_adress)

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67
  • This is not a field (since that should at least be a field *instance*. Hence the error makes sense. – Willem Van Onsem Jan 15 '20 at 10:47
  • @WillemVanOnsem how should i approach this ? since i don't want to migrate a new field i would like to just show the decrypted data when i call `user. eth_address_decrypted` on that object – Linh Nguyen Jan 15 '20 at 10:49
  • You can just define a regular python class property in your model where you return decrypted data. But you wont be able to make query with that as you are trying to do. – Nafees Anwar Jan 15 '20 at 11:15

0 Answers0