How to store NFT wallet addresses in Django models?

Hey everyone! I’m working on a Django project that deals with NFTs. I’m stuck on how to properly store wallet addresses in my models. These addresses are usually hex strings, but I’m not sure about the best field type to use.

I was thinking maybe a custom CharField would work, but I don’t know what length to set. Are there any standard lengths for NFT wallet addresses?

I’ve ruled out UUIDField and TextField already. Has anyone done this before? What worked for you? Any tips or code snippets would be super helpful!

Thanks in advance for your help. I’m really excited to get this part of my project figured out!

I’ve implemented something similar in a recent project. For NFT wallet addresses, a CharField with max_length=64 is a good choice. It accommodates most blockchain address formats while providing some buffer.

Here’s a basic model setup I’ve used:

from django.db import models
from django.core.validators import RegexValidator

class NFTWallet(models.Model):
    address = models.CharField(
        max_length=64,
        validators=[RegexValidator(regex='^0x[a-fA-F0-9]{40}$')]
    )

The RegexValidator ensures the address follows the standard Ethereum format. You might need to adjust this for other blockchains.

Consider adding an index to the address field if you’ll be querying it frequently. Also, think about how you’ll handle address normalization (lowercase vs. uppercase) to prevent duplicates.

hey nina! i’ve actually been working on something similar recently. for nft wallet addresses, i found that using a CharField with a max_length of 42 works pretty well. most ethereum-style addresses are 42 characters long (including the ‘0x’ prefix).

here’s a quick snippet i use:

from django.db import models

class NFTWallet(models.Model):
    address = models.CharField(max_length=42)
    # other fields...

but heres the thing - what if you’re dealing with different blockchain networks? some might have longer addresses. so maybe consider adding a little buffer, like max_length=50 just to be safe?

oh, and dont forget to add some validation! you could create a custom validator to check if the address format is correct. that would be super helpful.

what kind of nft project are you working on? sounds interesting! have you thought about how youre going to handle transactions and stuff?

yo nina, i’ve dealt with this b4. CharFields work great, but don’t forget bout other chains! some have longer addresses. i usually go with max_length=64 to be safe.

also, add a validator to check address format. helps catch typos n stuff.

wut kind of nft project u workin on? sounds cool!