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!!
userfor yourUserMobileDevice. Furthermore note that thetoken, etc. can not beNone, hencevalidated_data.get('token', None), does not make much sense.