(DRF) Why make a query before create when unique=True?

Hi there!

I have User model and serializer class which represents it.

class User(models.Model):
username = models.CharField(unique=True, max_length=50)
is_admin = models.BooleanField(default=False)

class CreateUserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (“username”, “is_admin”)

def create(self, validated_data: Dict[str, Any]) -> User:
    username = validated_data["username"]

    try:
        user_object = User.objects.create(username=username)
    except IntegrityError:
        raise UserAlreadyRegistered()

    return user_object

My database holds lots of users(appr. 10 million). When I create another user, Django Rest Framework look at the model fields and check that if there are any unique=True fields. If there are, then it assigns “UniqueValidator”. This validator calls “filter_queryset” which means it query the database and check that are there any given username record. If there are not, then it creates the user.

My question is, why DRF make a query before create operation? Since database(PostgreSQL) holds the field attributes, it knows that username field is unique, and it can throw an exception if I try to create user with the same username field. The reason I’m asking it, my application gets a lot of create operation, and each create operation I have to query my database, which is I think more costly than create it wihout making a filter query. Do I missing something in this operation?

Thanks!