Django Rest Framework - Set current user on model with unique constraint - django

I have a model that represents a device identifier and I'd like to create a unique constraint on the device identifier and the current user.
I was passing the user on the save method but I had to remove the constraint and now that I'm trying to write tests the poor code that I wrote becomes difficult to test. How can I write a serializer where I could set the currentuser as default and mantain the unique contrasint on the model.
This is my model
class DeviceIdentifier(models.Model):
owner = models.ForeignKey(HDSAuthUser, primary_key=True, on_delete=models.CASCADE)
id_device = models.UUIDField(primary_key=True)
insert_date = models.DateTimeField(auto_now_add=True)
and this is my serializer
class DeviceIdentifierSerializer(serializers.ModelSerializer):
"""
Device identifier serializer
"""
class Meta:
model = DeviceIdentifier
fields = ('id_device', 'owner')
and this is my view
class RegisterDevice(CreateAPIView):
"""
View for registering device for the logged user
"""
serializer_class = DeviceIdentifierSerializer
def post(self, request, *args, **kwargs):
obj = DeviceIdentifierSerializer(data=request.data)
obj.is_valid()
obj.save(owner=request.user)
return Response(True)

Add Unique constaint on models instead of Model Serializer, it will surely work.
class DeviceIdentifier(models.Model):
owner = models.ForeignKey(HDSAuthUser, on_delete=models.CASCADE)
id_device = models.UUIDField(primary_key=True)
insert_date = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('field1', 'field2',)
class DeviceIdentifierSerializer(serializers.ModelSerializer):
"""
Device identifier serializer
"""
class Meta:
model = DeviceIdentifier

Related

Nested ManytoMany Serialization in Django Rest Framework

In my app I have a nested many to many relation like the following:
Models.py
class ReturnKitsProducts(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
class ReturnKits(models.Model):
kit = models.ForeignKey(Kit, on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
items = models.ManyToManyField(ReturnKitsProducts)
class Return(models.Model):
transaction_date = models.DateTimeField(default=datetime.now)
transaction_no = models.IntegerField(default=0, blank=True, null=True)
flow = models.ForeignKey(Flow, on_delete=models.CASCADE)
kits = models.ManyToManyField(ReturnKits)
warehouse = models.ForeignKey(Warehouse, on_delete=models.CASCADE)
In this ReturnKitsProducts is connected to ReturnKits as M2M and ReturnKits is connected to Return as M2M. I have handles only single level of M2M serialization for updatation and creation like this:
Serializers.py
class ReturnKitsSerializer(serializers.ModelSerializer):
class Meta:
model = ReturnKits
fields = "__all__"
class ReturnSerializer(serializers.ModelSerializer):
kits = ReturnKitsSerializer(many=True)
class Meta:
model = Return
fields = "__all__"
def create(self, validated_data):
items_objects = validated_data.pop('kits', None)
prdcts = []
for item in items_objects:
i = ReturnKits.objects.create(**item)
prdcts.append(i)
instance = Return.objects.create(**validated_data)
print("prdcts", prdcts)
instance.items.set(prdcts)
return instance
But I am not sure how to do serialization in the above mentioned scenario. Please Help!!
You can try something like this:
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = ReturnKitsProducts
fields = "__all__"
class ReturnKitsSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True) # same as
class Meta:
model = ReturnKits
fields = "__all__"
class ReturnSerializer(serializers.ModelSerializer):
kits = ReturnKitsSerializer(many=True)
class Meta:
model = Return
fields = "__all__"
def create(self, validated_data):
items_objects = validated_data.pop('kits', None)
instance = Return.objects.create(**validated_data)
for item in item_objects:
return_products = item.pop('items')
i = ReturnKits.objects.create(**item)
for data in return_products:
return_product = ReturnKitsProducts.objects.create(**data)
i.items.add(return_product)
instance.items.add(i)
return instance
What I did was pulling out the data from the validated_data dictionary and create instances as necessary.
While the other answer works (for create and get), it is always better to use serializers to save data when possible. In future you may need some kind of custom validation or you may need to override default create method, for nested serializer.
If you don't do following, you will miss out all functions serializer class provides and may need to write your all code on your own in create function of your main serializer, which will be dirty.
class ItemSerializer(serializers.ModelSerializer):
class Meta:
model = ReturnKitsProducts
fields = "__all__"
class ReturnKitsSerializer(serializers.ModelSerializer):
items = ItemSerializer(many=True) # same as
class Meta:
model = ReturnKits
fields = "__all__"
def validate_items(self, value):
serializer = ItemSerializer(data=value, many=True)
if serializer.is_valid():
return serializer # this will be finally received and used in create function of main serializer
else:
raise serializers.ValidationError(serializer.errors)
def create(self, validated_data):
items = validated_data.pop('items')
r = ReturnKits.objects.create(**validated_data)
for item in items:
r.items.add(item.save()) # save each serializer (already validated) and append saved object
return r
class ReturnSerializer(serializers.ModelSerializer):
kits = ReturnKitsSerializer(many=True)
def validate_kits(self, value):
serializer = ReturnKitsSerializer(data=value, many=True)
if serializer.is_valid():
return serializer
else:
raise serializers.ValidationError(serializer.errors)
class Meta:
model = Return
fields = "__all__"
def create(self, validated_data):
# IMP: At this point all the data (including nested) will have been validated, so no error will throw when saving the data
kits = validated_data.pop('kits', None)
instance = Return.objects.create(**validated_data)
for kit in kits:
i = kits.save() # save each serializer (already validated) and get a object
instance.kits.add(i)
return instance
This code will work for create and get. If you want to update all your data at once, you should develop a approach.
You can try approach I use:
Updating data with its nested data
If the dictionary contains id field, the corresponding nested data will be updated.
If the dictionary does not contain id, field , new nested data will be created.
If the dictionary contains only id as key, the nested data will be deleted.
There is no need to provide nested data which do not need to be updated or deleted.
https://github.com/SagarKAdhikari/drf-nested-relations : A library where you can validate and save/update nested data to any depth, though it works for generic relation and foreign keys only for now( since only that was required in my project). You can try understanding the code and implment, if your nested relations are much deeper than at current and you have too many of them.

Invalid data. Expected a dictionary, but got str error with serializer field in Django Rest Framework

I'm using Django 2.x and Django REST Framework.
I have two models like
class Contact(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(User, on_delete=models.PROTECT)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, blank=True, null=True)
modified = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
class AmountGiven(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
contact = models.ForeignKey(Contact, on_delete=models.PROTECT)
amount = models.FloatField(help_text='Amount given to the contact')
given_date = models.DateField(default=timezone.now)
created = models.DateTimeField(auto_now=True)
the serializer.py the file has serializers defined as
class ContactSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Contact
fields = ('id', 'first_name', 'last_name', 'created', 'modified')
class AmountGivenSerializer(serializers.ModelSerializer):
contact = ContactSerializer()
class Meta:
model = AmountGiven
depth = 1
fields = (
'id', 'contact', 'amount', 'given_date', 'created'
)
views.py
class AmountGivenViewSet(viewsets.ModelViewSet):
serializer_class = AmountGivenSerializer
def perform_create(self, serializer):
save_data = {}
contact_pk = self.request.data.get('contact', None)
if not contact_pk:
raise ValidationError({'contact': ['Contact is required']})
contact = Contact.objects.filter(
user=self.request.user,
pk=contact_pk
).first()
if not contact:
raise ValidationError({'contact': ['Contact does not exists']})
save_data['contact'] = contact
serializer.save(**save_data)
But when I add a new record to AmountGiven model and passing contact id in contact field
it is giving error as
{"contact":{"non_field_errors":["Invalid data. Expected a dictionary, but got str."]}}
When I remove contact = ContactSerializer() from AmountGivenSerializer, it works fine as expected but then in response as depth is set to 1, the contact data contains only model fields and not other property fields defined.
I'm not a big fan of this request parsing pattern. From what I understand, you want to be able to see all the contact's details when you retrieve an AmountGiven object and at the same time be able to create and update AmountGiven by just providing the contact id.
So you can change your AmountGiven serializer to have 2 fields for the contact model field. Like this:
class AmountGivenSerializer(serializers.ModelSerializer):
contact_detail = ContactSerializer(source='contact', read_only=True)
class Meta:
model = AmountGiven
depth = 1
fields = (
'id', 'contact', 'contact_detail', 'amount', 'given_date', 'created'
)
Note that the contact_detail field has a source attribute.
Now the default functionality for create and update should work out of the box (validation and everything).
And when you retrieve an AmountGiven object, you should get all the details for the contact in the contact_detail field.
Update
I missed that you need to check whether the Contact belongs to a user (however, I don't see a user field on your Contact model, maybe you missed posting it). You can simplify that check:
class AmountGivenViewSet(viewsets.ModelViewSet):
serializer_class = AmountGivenSerializer
def perform_create(self, serializer):
contact = serializer.validated_data.get('contact')
if contact.user != self.request.user:
raise ValidationError({'contact': ['Not a valid contact']})
serializer.save()
Override the __init__() method of AmountGivenSerializer as
class AmountGivenSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
super(AmountGivenSerializer, self).__init__(*args, **kwargs)
if 'view' in self.context and self.context['view'].action != 'create':
self.fields.update({"contact": ContactSerializer()})
class Meta:
model = AmountGiven
depth = 1
fields = (
'id', 'contact', 'amount', 'given_date', 'created'
)
Description
The issue was the DRF expects a dict like object from contact field since you are defined a nested serializer. So, I removed the nested relationship dynamically with the help of overriding the __init__() method
For those who got here but have relatively simple serializers, this error can also occur when the request data is malformed, in my case JSON encoded twice.
The serializer will decode the JSON, but as it is encoded twice request.data will still be a string. The error therefore makes sense as a "dictionnary" was expected, but we still have a "string".
You can check the output of the following to confirm whether this is the issue you are experiencing:
print(type(request.data)) # Should be <class 'dict'>

Related Object serializer Django restframework

My question is somewhat related to this one with some differences. I have a model similar to this one:
class Project(models.Model):
project_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, unique=True)
created_by_id = models.ForeignKey('auth.User', related_name='project', on_delete=models.SET_NULL, blank=True, null=True)
created_by = models.CharField(max_length=255, default="unknown")
created = models.DateTimeField(auto_now_add=True)
With the following serializer:
class ProjectSerializer(serializers.ModelSerializer):
created_by = serializers.ReadOnlyField(source='created_by_id.username')
class Meta:
model = Project
fields = ('project_id', 'created_by', 'created')
And corresponding view:
class projectsView(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def perform_create(self, serializer):
serializer.save(created_by_id=self.request.user)
This code behaves like I want but forces information redundancy and does not leverage the underlying relationnal database. I tried to use the info from the linked question to achieve a "write user id on database but return username on "get"" in a flat json without success:
Removing the "created_by" field in the model. Replacing the serializer with:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
class ProjectSerializer(serializers.ModelSerializer):
created_by = UserSerializer(read_only=True)
created_by_id = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), source='created_by', write_only=True)
class Meta:
model = Project
fields = ('project_id', 'created_by', 'created_by_id', 'created')
Which would NOT 100% give me what I want, i.e. replace the user id with the username in a flat json but return something like: {'project_id': <uuid>, 'created_by': <user json object>, 'created': <data>}. But still I get a {'created_by_id': ['This field is required.']} 400 error.
Question: How can I write a user id to a database object from the request.user information to refer to an actual user id but return a simple username in the GET request on the projectsView endpoint without explicitly storing the username in the Model? Or more generally speaking, how can I serialize database objects (Django models) into customer json response by using default serialization DRF features and default DRF views mixins?
Alternate formulation of the question: How can I store an ID reference to another DB record in my model (that can be accessed without it being supplied by the payload) but deserialize a derived information from that object reference at the serializer level such as one specific field of the referenced object?
I would recommend you to use Two different serializers for Get and POST operations. Change your serializers.py as
class ProjectGetSerializer(serializers.ModelSerializer):
created_by_id = serializers.StringRelatedField()
class Meta:
model = Project
fields = '__all__'
class ProjectCreateSerializer(serializers.ModelSerializer):
created_by_id = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), default=serializers.CurrentUserDefault())
def create(self, validated_data):
return Project.objects.create(**validated_data, created_by=validated_data['created_by_id'].username)
class Meta:
model = Project
fields = '__all__'
Also, I reccomend ModelViewSet for API class if you are looking for CRUD operations. Hence the view will be like this,
class projectsView(viewsets.ModelViewSet):
queryset = Project.objects.all()
def get_serializer_class(self):
if self.action == 'create':
return ProjectCreateSerializer
return ProjectGetSerializer
So, the payload to create Project is,
{
}
One thing you should remember, while you trying to create Project user must logged-in
UPDATE - 1
serializer.py
class ProjectCreateSerializer(serializers.ModelSerializer):
created_by_id = serializers.StringRelatedField()
class Meta:
model = Project
fields = '__all__'
def create(self, validated_data):
return Project.objects.create(**validated_data, created_by_id=self.context['request'].user)
views.py
class projectsView(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectCreateSerializer
The error is in the write_only field options. The required parameter default value is set to True while the intent is to not make it required if we take a look at the model. Here in the view, I use the perform_create as post processing to save on the Model DB representation. Since required default value is True at the creation level, the first .save() to the DB fails. Since this is purely internal logic, the required is not necessary. So simply adding the required=False option on the PrimaryKeyRelatedField does the job:
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
class ProjectSerializer(serializers.ModelSerializer):
created_by = UserSerializer(read_only=True)
created_by_id = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), source='created_by', write_only=True, required=False)
class Meta:
model = Project
fields = ('project_id', 'created_by', 'created_by_id', 'created')
Enforcing the required=True at the Model level as well would require to override the .save function of the serializer if I insist on playing with the logic purely at the serializer level for deserialization. There might be a way to get the user ref within the serializer as well to keep the views implementation even more 'default'... This can be done by using the default value from Jerin:
class ProjectSerializer(serializers.ModelSerializer):
created_by = UserSerializer(read_only=True)
created_by_id = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), source='created_by',
write_only=True,
required=False,
default=serializers.CurrentUserDefault())
class Meta:
model = Project
fields = ('project_id', 'created_by', 'created_by_id', 'created')
Now to flaten the json with username only, you need to use a slug field instead of the UserSerializer:
class ProjectSerializer(serializers.ModelSerializer):
created_by = serializers.SlugRelatedField(
queryset=User.objects.all(), slug_field="username")
created_by_id = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), source='created_by', write_only=True, required=False)
class Meta:
model = Project
fields = ('project_id', 'created_by', 'created_by_id', 'created')
And then only the username field value of the User Model will show at the create_by json tag on the get payload.
UPDATE - 1
After some more tweaking here is the final version I came up with:
class ProjectSerializer(serializers.ModelSerializer):
created_by_id = serializers.PrimaryKeyRelatedField(
queryset=User.objects.all(), write_only=True, required=False, default=serializers.CurrentUserDefault())
created_by = serializers.SerializerMethodField('creator')
def creator(self, obj):
return obj.created_by_id.username
class Meta:
model = Project
fields = ('project_id', 'created_by_id', 'created_by', 'created')

How to use Primary Key In a Serializer in Django

My Model is
class UserInfo(models.Model):
user = models.OneToOneField(User, unique=True)
mobile_no = models.CharField(max_length=10, blank=True)
and serialzer is :
class UserInfoSerializer(serializers.ModelSerializer):
class Meta:
model = UserInfo
fields = ('mobile_no','user')
but whenever I tried to use this
serializer = UserInfoSerializer(data=data)
if serializer.is_valid():
serializer.save()
It is not saving the data and giving errors.
Is there any method to use other then this to for using Primary key.
You should use PrimaryKeyRelatedField
add this to your serializer
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
Your UserInfoSerializer should look like:
class UserInfoSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
class Meta:
model = UserInfo
fields = ('mobile_no','user')
Update
If you want to update existing object in database then you have to pass model instance as an argument to UserInfoSerializer constructor.
user_info = self.get_object()
serializer = UserInfoSerializer(user_info, data=data)

How to override model field validation in django rest framework ModelSerializer

I have the following model:
class UserProfile(models.Model):
mobileNumber = models.BigIntegerField(primary_key=True)
authKey = models.CharField(max_length=300,null=False,blank=False)
creationDateTime = models.DateTimeField(auto_now_add=True)
lastUpdateDateTime = models.DateTimeField(auto_now=True)
Serializer:
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = ('mobileNumber','authKey')
If userprofile model already has a mobilenumber XX44 and if I try to serialize using UserProfileSerializer with json {'mobileNumber': XX44, 'authKey': u'ggsdsagldaslhdkjashdjkashdjkahsdkjah'} I'm getting the following error:
{'mobileNumber': [u'User profile with this MobileNumber already exists.']}
because model validations are being run for the serializer field.
How can I stop execution of model field validation for mobileNumber. I have tried validate and validate_mobileNumber methods in serializer but they still are executing the model validations.
remove unique constraint on mobile number of table,so django serializer will validate according to that .
or
alternatively,
serializer=UserProfileSerializer(data=request.DATA,partial=True)
I understand you won't save the serializer data. So, you can set mobileNumber as a read_only field on UserProfileSerializer.
Check serializer fields documentation for more info: http://www.django-rest-framework.org/api-guide/fields/#core-arguments
By overriding the model field within the serializer, and specifying required=False, allow_blank=True, allow_null=True:
class SomeModel(models.Model):
some_model_field_which_is_required = models.ForeignKey(...)
some_other_required_field = models.CharField(...)
class SomeModelSerializer(serializers.ModelSerializer):
some_model_field_which_is_required = SomeNestedSerializer(
many=True, required=False, allow_blank=True
)
some_other_required_field = serializers.CharField(required=False, allow_blank=True)
def validate(self, *args, **kwargs):
print('should get here')
def validate_some_other_required_field(self, *args, **kwargs):
print('should also get here')
class Meta:
model = SomeModel