Flask, marshmallow - problem with nested field - python

I have 2 tables with "one-to-one" relationship.
When I get data from one table I want to include one field from the related table but without "dot notation".
What works:
class UserEntitySchema(db_schema.Schema):
class Meta:
fields = ('id', 'username', 'email', 'confirmed', 'created', 'enabled', 'account.status')
I want "account.status" would come as just "status" but I can't figure out how to gain it.
I tried Pluck as #marke suggested but without any result. Anything is wrong here?
class AccountEntitySchema(db_schema.Schema):
current_status = fields.Str()
class Meta:
fields =('current_status',)
class UserEntitySchema(db_schema.Schema):
status = fields.Pluck(AccountSchema, 'current_status')
class Meta:
fields = ('id', 'username', 'email', 'status')
My updated, working solution (thanks #marke!):
class AccountEntitySchema(db_schema.Schema):
class Meta:
fields =('current_status',)
class UserEntitySchema(db_schema.Schema):
account = fields.Pluck(AccountSchema, 'current_status', data_key='status') #<==== data_key will replace the existing field
class Meta:
# Fields to expose
fields = ('id', 'username', 'email', 'account')

As mentioned in the comments, one can use pluck field: https://marshmallow.readthedocs.io/en/stable/api_reference.html#marshmallow.fields.Pluck

Related

Create serializer fields according to the data in Django Rest

I am writing an api for Auth. and I have 2 user types ( Donors , Charity ) when they did registration my data is changing. So that I need to update my fields according to the data.
Here is what I did so far.
views.py
class RegisterView(APIView):
def post(self, request):
js_data = request.data
if 'last_name' in js_data:
#fields = ['id', 'email', 'first_name', 'last_name', 'password']
serializer = UserSerializer(data=js_data)
serializer.is_valid(raise_exception=True)
serializer.save()
else:
#fields = ['id', 'email', 'first_name', 'postcode', 'password']
serializer = UserSerializer(data=js_data)
serializer.is_valid(raise_exception=True)
serializer.save()
in views.py I tried to define fields from outside serializers.py but I can't send fields as a parameter.
serializer = UserSerializer(data=js_data, fields = fields) # gives error.
also I tried this one;
serializer = UserSerializer(data=js_data, many= True, fields = fields) # gives error
So I am trying to create fields dynamically.
serializer.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = []
extra_kwargs = {'password': {'write_only': True} }
def create(self, validated_data):
if 'last_name' in validated_data:
self.Meta.fields = ['id', 'email', 'first_name', 'last_name', 'password']
else:
self.Meta.fields = ['id', 'email', 'first_name', 'postcode', 'password']
password = validated_data.pop('password', None)
instance = self.Meta.model(**validated_data)
if password is not None:
instance.set_password(password)
instance.save()
return instance
So in here I tried to change fields in create function. I am not getting an error but in database my fields are empty :( Maybe I define fields = [] as a empty list thats why fields are empty and not saving any data of user.
Btw, I started to learn django rest. I new at this framework so that I am sorry to any false definition or etc..
So any ideas to create fields according the data? Thanks in advance. Have a nice day.
The best solution would be to have 2 separate serializers:
Make 2 different serializers, each with its own logic and fields
If some logic is shared between both serializers, simply create a parent class with the shared logic, and make them inherit from it
In your view, simply pick the serializer based on your need, like so:
if 'last_name' in js_data:
serializer = SerializerOne(data=js_data)
else:
serializer = SerializerTwo(data=js_data)
serializer.is_valid(raise_exception=True)
serializer.save()
Having 2 separate serializer will make the code cleaner and easier to understand and maintain

Filter query set using Django rest serializer

I have two serialiser and one of them is used in another.
class CommentSerializers(ModelSerializer):
class Meta:
model = Comment
fields = ['id','text', 'author','approved' , 'created_date']
read_only_fields = ['author' ,'approved' ,'created_date']
and then
class PostSerializers(ModelSerializer):
likes_count = serializers.SerializerMethodField(read_only=True)
comments = CommentSerializers(source='comment_set', many=True, read_only=True)
post_media = PostMediaSerializers(source='postmedia_set', many=True, required=False)
class Meta:
model = Post
fields = [
'id', 'text', 'likes_count', 'likes_count',
'post_media', 'author', 'approved', 'comments','created_date',
]
read_only_fields = ['author', 'approved', 'comments','created_date',]
As you see I get comments on a post and I nest it there so that every post comes with its comments. The challenge is getting is that I only wanted to show users approved posts. How can I filter the comments_set?

django-filter filter on annotated field

class EventViewSet(viewsets.ModelViewSet):
queryset = Event.objects.all()
serializer_class = EventSerializer
def get_queryset(self):
return super().get_queryset().annotate(
is_active=ExpressionWrapper(
Q(start_date__lt=timezone.now()) & Q(end_date__gt=timezone.now()),
output_field=BooleanField()
),
)
search_fields = [
'name',
'short_desc',
'desc',
]
filterset_fields = [
'is_active',
]
I have this ViewSet that I want to filter on an annotated field, normally you can simply just filter on the annotation in django querysets, however the above combined with this serializer:
class EventSerializer(serializers.ModelSerializer):
is_active = serializers.SerializerMethodField()
#staticmethod
def get_is_active(obj):
return obj.is_active
class Meta:
model = Event
fields = [
'timestamp',
'id',
'name',
'short_desc',
'desc',
'start_date',
'end_date',
'is_active',
]
I haven't looked deep into the source code but I'd assume it would do a simple qs.filter for the fields in filterset_fields but I'm getting this beautiful error that fails to explain much(at least to me):
'Meta.fields' contains fields that are not defined on this FilterSet: is_active
Apparently you simply don't add the declared filters on the Meta.fields list. Literally inside the docs and I found out about it by reading the code.
Also, when adding an annotated field to the declared_fields AKA the filterset class body, add a label or django-filters can't produce field label properly and just goes with "invalid name" instead of you know, the field name. Who knows why.

unique_together of two field messes with read_only_fields

I have this code for rating lessons, user and lesson should be added automatically from request authorization and URL:
#views.py
class RatingViewSet(
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet
):
permission_classes = [permissions.IsAuthenticated]
serializer_class = RatingSerializer
def perform_create(self, serializer):
lessonInstance = Lesson.objects.get(id = self.kwargs['lessonID'])
serializer.save(user=self.request.user, lesson = lessonInstance)
def get_queryset(self):
lessonID = self.kwargs['lessonID']
return Rating.objects.filter(user=self.request.user, lesson=lessonID)
#serializers.py
class RatingSerializer(serializers.ModelSerializer):
class Meta:
model = Rating
fields = ('id', 'lesson','user', 'difficulty')
read_only_fields = ('id', 'user','lesson')
#models.py
class Rating(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL)
lesson = models.ForeignKey('lessons.Lesson')
difficulty = models.IntegerField()
class meta:
unique_together('user','lesson')
I want to have max 1 rating per user/lesson, hence unique_together('user','lesson'). But there is a problem: as long as that constraint is in the code, requests without user or lesson fields get denied with field required error, even though they are read_only.
(If I migrate with unique_together('user','lesson'), then delete that line it works, but as soon as it's there I get errors.)
I want to keep that bit of code there so I don't accidentally remove the unique_together constraint on later migrations.
This is a special-case that requires a different approach. Here's what django-rest-framework documentation (see the Note) says about this case:
The right way to deal with this is to specify the field explicitly on
the serializer, providing both the read_only=True and default=…
keyword arguments.
In your case, you need to explicitly define the user and lesson fields on your RatingSerializer, like this:
class RatingSerializer(serializers.ModelSerializer):
user = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault()) # gets the user from request
lesson = serializers.PrimaryKeyRelatedField(read_only=True, default=None) # or any other appropriate value
class Meta:
model = Rating
fields = ('id', 'lesson','user', 'difficulty')
Good luck!
If a field is read_only=True then the validated_data will ignore data of it => Cause error required field, read more at doc
I also met this issue in a similar context, then tried #iulian's answer above but with no luck!
This combo read_only + default behavior is not supported anymore, check this
I resolved this issue by 2 solutions:
My model:
class Friendship(TimeStampedModel):
"""Model present Friendship request"""
from_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='friendship_from_user')
to_user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='friendship_to_user')
class Meta:
unique_together = ('from_user', 'to_user')
Solution 1. Write your own CurrentUserDefault class to get the user id then set to default attribute data of serializer(Ref from #51940976)
class CurrentUserDefault(object):
def set_context(self, serializer_field):
self.user_id = serializer_field.context['request'].user.id
def __call__(self):
return self.user_id
class FriendshipSerializer(serializers.ModelSerializer):
from_user_id = serializers.HiddenField(default=CurrentUserDefault())
class Meta:
model = Friendship
fields = ('id', 'from_user', 'from_user_id', 'to_user', 'status')
extra_kwargs = {
'from_user': {'read_only': True},
}
Solution 2. Override the create method of serializer to set data for user id(Ref from this)
class FriendshipSerializer(serializers.ModelSerializer):
class Meta:
model = Friendship
fields = ('id', 'from_user', 'to_user', 'status')
extra_kwargs = {
'from_user': {'read_only': True},
}
def create(self, validated_data):
"""Override create to provide a user via request.user by default.
This is require since the read_only `user` filed is not included by default anymore since
https://github.com/encode/django-rest-framework/pull/5886.
"""
if 'user' not in validated_data:
validated_data['from_user'] = self.context['request'].user
return super(FriendshipSerializer, self).create(validated_data)
I hope this helps!

Django REST Framework nested resource key "id" unaccessible

So I have the following Structure:
A ClientFile belongs to an Owner (class name = Contact).
I'm trying to create a Clientfile using the API. The request contains the following data:
{
name: "Hello!"
owner: {
id: 1,
first_name: "Charlie",
last_name: "Watson"
}
}
I created the serializer according to my structure. Hoping that this API call would create a clientfile with the name "Hello!" and Contact id 1 as the owner:
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = (
'id',
'first_name',
'last_name',
)
class ClientfileSerializer(serializers.ModelSerializer):
owner = ContactSerializer(read_only=False)
class Meta():
model = Clientfile
fields = (
'id',
'name',
'owner',
)
def create(self, validated_data):
owner = Contact.objects.get(pk=validated_data['owner']['id'])
I do get into the create method. However, the only field I need (['owner']['id']) is not accessible. If I do print ['owner']['first_name'] it does return 'Charlie'. But the ID for some reasons doesn't seem to be accessible...
Any reasons why this can be happening? Am i missing something? (I'm new to Django)
SOLUTION: Just found out that the reason why ID didn't show in the first place was because I had to declare it in the fields like so: Hope this helps.
class ContactSerializer(serializers.ModelSerializer):
id = serializers.IntegerField() # ← Here
class Meta:
model = Contact
fields = (
'id',
'first_name',
'last_name',
)
In Django REST Framework AutoField fields (those that are automatically generated) are defaulted to read-only. From the docs:
read_only
Set this to True to ensure that the field is used when
serializing a representation, but is not used when creating or
updating an instance during deserialization.
Defaults to False
You can see this by inspecting your serializer by printing the representation in your shell:
serializer = ClientfileSerializer()
print repr(serializer)
You can override this by setting read_only=False against the id field in the extra_kwargs:
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = (
'id',
'first_name',
'last_name',
)
extra_kwargs = {'id': {'read_only': False}}
class ClientfileSerializer(serializers.ModelSerializer):
owner = ContactSerializer(read_only=False)
class Meta():
model = Clientfile
fields = (
'id',
'name',
'owner',
)
extra_kwargs = {'id': {'read_only': False}}
Alright so I found a different approach that works.
I added an IntegerField serializer for the owner relation. I also had to set the owner relation to read_only=True.
This is the json I am sending via POST:
{
name: "Hello!"
owner_id: 1
}
This is my serializer:
class ClientfileSerializer(serializers.ModelSerializer):
owner_id = serializers.IntegerField()
owner = ContactSerializer(read_only=True)
class Meta():
model = Clientfile
fields = (
'id',
'owner_id',
'owner',
)
It seems less cool than the first way, but it does the job.
Plus I don't want to create a new owner, but just select one that is already in the database. So maybe it's more semantic to only have the ID and not the full set of information posted via Json.
You can try something like this:
class YourModelSerializer(serializers.ModelSerializer):
class Meta:
model = YourModel
fields = ('id', 'field1', 'field2')
def to_internal_value(self, data):
"""
Dict of native values <- Dict of primitive datatypes.
Add instance key to values if `id` present in primitive dict
:param data:
"""
obj = super(YourModelSerializer, self).to_internal_value(data)
instance_id = data.get('id', None)
if instance_id:
obj['instance'] = YourModel.objects.get(id=instance_id)
return obj
Then in serializer validated data you should have "instance" key if request.data has "id" key.
Also You can add just "id" instead of full instance/object.
The top voted answer does solve the issue but it raises a new one as mentioned in the comments we can no longer create a new record as it will thrown ac exception saying is required. We can set id to required=False then id will be available in validated_data and it wont be required to set it manually
id = serializers.IntegerField(required=False) <- Like this
class Meta:
model = Details
fields = ('id', 'product_name', 'description', 'specification', 'make_model',
'brand', 'quantity',)

Categories

Resources