I'm building a Django app that involves working with NFTs, and I want to know the best way to represent NFT wallet addresses using a Django model field.
NFT wallet addresses are typically hexadecimal strings, but I'm not sure how to design the model field to accommodate them efficiently. I think the best option is a customization of the CharField with a length constraint to store the hexadecimal address. But is there a recommended maximum length for NFT wallet addresses? I'd exclude UUIDField and TextField.
I'd appreciate any examples and recommendations. Thank you!
At the end I came up with the following solution, I post it here since it could be helpful for the community:
from django.core.validators import RegexValidator
from django.db import models
class UserProfile(models.Model):
wallet_address_eth = models.CharField(
verbose_name="Ethereum Wallet Address",
max_length=42,
unique=True,
validators=[RegexValidator(regex=r'^0x[a-fA-F0-9]{40}$')],
)
# ...
The regex on the model field ensures that the wallet_address_eth
is limited to 42 chars and follows the Ethereum wallet address format.
Keep in mind that this approach is specific to Ethereum addresses, and if your application needs to support other blockchain networks with different address formats, you may need to adjust the len and regex accordingly.