My goal is that the users of my application can create objects of the following type:
class Wallet(models.Model):
name = models.ChatField(unique=True)
Each user is assigned the id of the Wallet that they have created themselves, so they will only be able to see their own wallets, but since the name is unique, if a user tries to create a wallet with a name that already exists, it will fail.
How can I make it so that the user cannot create a wallet with a name that already exists among all the wallets he has created, but that name can exist in a Wallet created by another user?
I understand that the unique=True should be removed and I suppose that it would be controlled by the views or the form in the following way:
I check all the wallets that the user has created, if there is one that has that name, in that case, I return an error.
Is there any other more efficient way?
Thanks.
Perhaps you could store the name of the Wallet internally as: username_name_wallet_name or something similar.
It would only be at the level of saving them. When the user sees them, you would remove the username before. In the same way, when the user creates a new one, he will write it without his name in front of it and you will simply add the name before saving it.
To avoid collisions with other usernames (for example the user "dog" creates a wallet "my_wallet_1" (dog_my_wallet_1) we would have to prevent a possible user "dog_my" from creating "wallet_1"
One option is to use a separator that you do not allow in the name of the wallet. like/or @
I hope I understood you and that it helps you