SJ19
SJ19

Reputation: 2123

Django Rest Framework - Unique field, but still allows multiple objects with the same value

I'm trying to make the field "mollie_cid" unique, such that when there exists an account with a certain mollie_cid, another account can not sign up with the same mollie_cid. This is not the case currently, I can make multiple accounts with the same mollie_cid currently.

I added "unique=True", but I can still register multiple accounts with the same mollie_cid

Does anyone know how to make it truly unique?

Model

class CustomUser(AbstractBaseUser):
    ...
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True)
    
    mollie_cid = models.CharField(max_length=255, blank=False, null=False, unique=True)
    ...

Serializer

class CustomRegisterSerializer(RegisterSerializer):
        ...
        is_admin = serializers.BooleanField(default=True)
        mollie_cid = serializers.CharField(max_length=255, default=None) # Set default when its not required on signup

        def get_cleaned_data(self):
            data_dict = super().get_cleaned_data()
            ...
            data_dict['is_admin'] = self.validated_data.get('is_admin', False)
            data_dict['mollie_cid'] = self.validated_data.get('mollie_cid', None)
            return data_dict

enter image description here

enter image description here

Upvotes: 0

Views: 33

Answers (1)

Amir Afianian
Amir Afianian

Reputation: 2785

After making a change to your model fields, you have to issue these two commands:

python manage.py makemigrations
python manage.py migrate

Upvotes: 2

Related Questions