0

How to check if the data exist in the Database, If data not exist then it will create or else return. I have used get_or_create() but somehow i am getting the error

IntegrityError at NOT NULL constraint failed

Model Class

class UserMobileDevice(models.Model):
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE
    )
    token = models.CharField(max_length=255)
    device_model = models.CharField(max_length=255)
    os = models.CharField(max_length=255)
    os_version = models.IntegerField(null=True)
    build_version = models.CharField(max_length=255, null=True)
    manufacturer = models.CharField(max_length=255, null=True)
    device = models.CharField(max_length=255, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

Serializer class

class UserMobileSerializer(serializers.ModelSerializer):
    #user = serializers.StringRelatedField()
    class Meta:
        model = models.UserMobileDevice
        fields = '__all__'
        read_only_fields = ('id',)

    def create(self, validated_data):
        user, created = models.UserMobileDevice.objects.get_or_create(
            token=validated_data.get('token', None),
            defaults={'token': validated_data.get('token',None),'os':validated_data.get('os',None),'device_model':validated_data.get('device_model',None),'os_version':validated_data.get('os_version',None),'build_version':validated_data.get('build_version',None),'manufacturer':validated_data.get('manufacturer',None),'device':validated_data.get('device',None)})
        return user

View class

class UserMobileViewSet(viewsets.ModelViewSet):
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UserMobileDevice.objects.all()
    serializer_class = UserMobileSerializer

    def get_queryset(self):
        return self.queryset.filter(user=self.request.user)

I am not very sure why I am getting this following error. Any help will be highly appreciated!!

2
  • You did not set the user for your UserMobileDevice. Furthermore note that the token, etc. can not be None, hence validated_data.get('token', None), does not make much sense. Commented Dec 3, 2019 at 10:34
  • @WillemVanOnsem Can you help me with rewrite that class how to achieve my task Commented Dec 3, 2019 at 10:35

2 Answers 2

1

You can try somethings like this in your serializer:

class UserMobileSerializer(serializers.ModelSerializer):
    #user = serializers.StringRelatedField()
    class Meta:
        model = models.UserMobileDevice
        fields = '__all__'
        read_only_fields = ('id', 'user')

    def create(self, validated_data):
        instance, created = self.Meta.model.objects.get_or_create(**validated_data)
        if not created:
            raise ValidationError('instance alreaady exists..')
        return instance

and in your view:

class UserMobileViewSet(viewsets.ModelViewSet):
    authentication_classe(authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)
    queryset = UserMobileDevice.objects.all()
    serializer_class = UserMobileSerializer

    def get_queryset(self):
        return self.queryset.filter(user=self.request.user)

    def perform_create(self, serializer):
        user = self.request.user
        serializer.save(user=user)
Sign up to request clarification or add additional context in comments.

Comments

0

You are getting constraint error because the fields have been defined not to be null and you might be populating a null value in it.

There are two major issues here:

  1. User is a foreign key to AUTH_USER_MODEL, but no instance for the same is created when you are populating using get_or_create. Based on the code snippets you shared, you dont actually need to have a foreign key to user model, instead you can just extend the user model.

        from django.contrib.auth.models import User
    
        class UserMobileDevice(User):
            # Your code
    
  2. Secondly, your model defines fields like token etc as Non null-able fields. So, using None to populate data makes no sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.