Reputation: 599
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!!
Upvotes: 0
Views: 1073
Reputation: 1551
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)
Upvotes: 1
Reputation: 1057
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:
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
Secondly, your model defines fields like token etc as Non null-able fields. So, using None to populate data makes no sense.
Upvotes: 0