I am working on an API, with Django, Django Rest Framework, and trying to achieve these(ad described)
First Serializer
class DeviceConfigSerializer(serializers.ModelSerializer):
config = serializers.JSONField(initial={})
context = serializers.JSONField(initial={})
templates = FilterTemplatesByOrganization(many=True)
class Meta:
model = Config
fields = ['backend', 'status', 'templates', 'context', 'config']
extra_kwargs = {'status': {'read_only': True}}
Now I have two nested serializer containing the above serializer for the LIST and DETAIL endpoints:-
Second Serializer
class DeviceListSerializer(FilterSerializerByOrgManaged, serializers.ModelSerializer):
config = DeviceConfigSerializer(write_only=True, required=False)
class Meta(BaseMeta):
model = Device
fields = ['id','name','organization','mac_address','key','last_ip','management_ip',
'model', 'os', 'system', 'notes', 'config', 'created', 'modified',]
Third Serializer
class DeviceDetailSerializer(BaseSerializer):
config = DeviceConfigSerializer(allow_null=True)
class Meta(BaseMeta):
model = Device
fields = ['id','name','organization','mac_address','key','last_ip','management_ip',
'model','os','system','notes','config','created','modified',]
Now, I am using the same DeviceConfigSerializer serializer for List, and Detail endpoint, but for the list endpoint I have set the nested serializer as write_only=True, But What I am trying to do with the list endpoint that is DeviceListSerializer serilaizer is that out of all the fields from the DeviceConfigSerializer, I want the status, and backend fields to be both read & write and others fields as write_only.
Presently with this configuration I am getting the response from the DeviceListSerializer as this:-
{
"id": "12",
"name": "tests",
"organization": "-------",
"mac_address": "-------",
"key": "------",
"last_ip": null,
"management_ip": null,
"model": "",
"os": "",
"system": "",
"notes": "",
"created": "2021-04-26T10:41:25.399160+02:00",
"modified": "2021-04-26T10:41:25.399160+02:00"
}
What I am trying to achieve is:-
{
"id": "12",
"name": "tests",
"organization": "----",
"mac_address": "-----",
"key": "----",
"last_ip": null,
"management_ip": null,
"model": "",
"os": "",
"system": "",
"notes": "",
"config": {
"status": "...",
"backend": "...",
,}
"created": "2021-04-26T10:41:25.399160+02:00",
"modified": "2021-04-26T10:41:25.399160+02:00"
}
PS: I tried by introducing an extra serializer for this two fields and nest it to the DeviceListSerializer, but I don't want to introduce an extra serializer for this two fields, and looking forward if this could be achieved with the same nested serializer.
Every Device instance contains config.
In short:-
I am trying to use the same DeviceConfigSerializer, for both DeviceListSerilaizer, and DeviceDetailSerializer. But for the DeviceListSerializer I want the status, & backend field from the DeviceConfigSerializerto be both read & write, which is presenty set to write only for the DeviceListSerializer.
This is not possible, so I had to introduce two new serializers for my need.
PrimaryKeyRelatedField may be used to represent the target of the relationship using its primary key
For example, the following serializer:
class AlbumSerializer(serializers.ModelSerializer):
tracks = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = Album
fields = ['album_name', 'artist', 'tracks']
Would serialize to a representation like this:
{
'album_name': 'Undun',
'artist': 'The Roots',
'tracks': [
89,
90,
91,
...
]
}
You can use this type of approach in your serializes. Reference https://www.django-rest-framework.org/api-guide/relations/#primarykeyrelatedfield
Related
in my post requests to OrderProduct model, i want to only have to pass order.id and product.id and it works... untill i add a serializer to retrieve product.name. It might be because i didnt understand documentation about nested requests, but im unable to advance further into my project :(
[
{
"id": 2,
"order": 1,
"product": 1,
}
]
^ here's how it looks without nested serializer, and thats the data that i wanna have to input
[
{
"id": 2,
"order": 1,
"product": {
"id": 1,
"name": "gloomhaven",
},
},
^ here's how it looks after i add an additional serializer. I pretty much want these nested fields to be read only, with me still being able to send simple post requests
here are my serializers
class OrderProductSerializer(serializers.ModelSerializer):
product = Product()
class Meta:
model = OrderProduct
fields = [
"id",
"order",
"product"]
class Product(serializers.ModelSerializer):
class Meta:
model = Product
fields = (
"id",
"name")
Is there any way for me to accomplish this? Thank you for trying to help!
Just overwrite to_representation method of the serializer
def to_representation(self, instance):
response = super().to_representation(instance)
response['other_field'] = instance.id# also response['other_field'] = otherSerializer(instance.model)
return response
This can solve your problem
I think you are missing many=True
class OrderProductSerializer(serializers.ModelSerializer):
product = Product(many=True)
class Meta:
model = OrderProduct
fields = [
"id",
"order",
"product"]
Is it possible to specify in which order fields will appear in a serialised model?
To make sure there is no confusion, while searching answers for this I have found lots of suggestions for ordering objects in a list view but this is not what I am looking for.
I really mean for a given model, I'd like their fields, once serialized to appear in a specific order. I have a fairly complex serialized object containing a lot of nested serializers, which appear first. I'd prefer instead key identifying fields such as name and slug to show up first, for readability.
Apologies in advance if this question is a duplicate, but I didn't find any relevant responses.
Solution
Based on #Toni-Sredanović solution I have implemented the following solution
def promote_fields(model: models.Model, *fields):
promoted_fields = list(fields)
other_fields = [field.name for field in model._meta.fields if field.name not in promoted_fields]
return promoted_fields + other_fields
class MySerializer(serializers.ModelSerializer):
...
class Meta:
model = MyModel
fields = promote_fields(model, 'id', 'field1', 'field2')
For that you can specify which fields you want to show and their order in class Meta:
class Meta:
fields = ('id', 'name', 'slug', 'field_1', 'field_2', ..., )
Here is a full example:
class TeamWithGamesSerializer(serializers.ModelSerializer):
"""
Team ModelSerializer with home and away games.
Home and away games are nested lists serialized with GameWithTeamNamesSerializer.
League is object serialized with LeagueSerializer instead of pk integer.
Current players is a nested list serialized with PlayerSerializer.
"""
league = LeagueSerializer(many=False, read_only=True)
home_games = GameWithTeamNamesSerializer(many=True, read_only=True)
away_games = GameWithTeamNamesSerializer(many=True, read_only=True)
current_players = PlayerSerializer(many=True, read_only=True)
class Meta:
model = Team
fields = ('id', 'name', 'head_coach', 'league', 'current_players', 'home_games', 'away_games', 'gender')
And the result:
{
"id": 1,
"name": "Glendale Desert Dogs",
"head_coach": "Coach Desert Dog",
"league": {
"id": 1,
"name": "Test league 1"
},
"current_players": [
{
"id": "rodriem02",
"first_name": "Emanuel",
"last_name": "Rodriguez",
"current_team": 1
},
{
"id": "ruthba01",
"first_name": "Babe",
"last_name": "Ruth",
"current_team": 1
}
],
"home_games": [
{
"id": 6,
"team_home": {
"id": 1,
"name": "Glendale Desert Dogs"
},
"team_away": {
"id": 2,
"name": "Mesa Solar Sox"
},
"status": "canceled",
"date": "2019-10-01"
},
{
"id": 7,
"team_home": {
"id": 1,
"name": "Glendale Desert Dogs"
},
"team_away": {
"id": 2,
"name": "Mesa Solar Sox"
},
"status": "",
"date": "2019-10-04"
}
],
"away_games": [
{
"id": 3,
"team_home": {
"id": 2,
"name": "Mesa Solar Sox"
},
"team_away": {
"id": 1,
"name": "Glendale Desert Dogs"
},
"status": "canceled",
"date": "2019-10-02"
}
],
"gender": "M"
}
If you would just use fields = '__all__' default ordering would be used which is:
object id
fields specified in the serializer
fields specified in the model
Best i can think of right now regarding your comment about generating fields is getting the fields in model, not really sure how to access what you've defined in the serializer so you would still need to write that manually.
Here is how you could do it with my example (this would make the name and gender appear on top):
class Meta:
model = Team
fields = ('name', 'gender')\
+ tuple([field.name for field in model._meta.fields if field.name not in ('name', 'gender')])\
+ ('league', 'home_games', 'away_games', 'current_players')
Suppose for below ModelSerializer class
class UserSongSerializer(serializers.ModelSerializer):
user = serializers.SerializerMethodField()
song_likes = serializers.ReadOnlyField() # This is model's property field
song_shares = serializers.ReadOnlyField()
song_plays = serializers.ReadOnlyField()
song_price = serializers.ReadOnlyField()
genre = GenreSerializer(many=True,required=False,context={'key':5})
language = LanguageSerializer(many=True, required=False)
Passing specific context kwarg like below
genre = GenreSerializer(many=True,required=False,context={'fields':['name']})
Since I want to retrieve only name field in Genre model class in some specific cases, I overrided GenreSerializer class's get_fields_name method so that I can mention specific fields only when required via context
class GenreSerializer(serializers.ModelSerializer):
def get_field_names(self, *args, **kwargs):
"""
Overriding ModelSerializer get_field_names method for getting only specific fields in serializer when mentioned in SerializerClass arguments
"""
field_names = self.context.get('fields', None)
if field_names:
return field_names
return super(GenreSerializer, self).get_field_names(*args, **kwargs)
class Meta:
model = Genre
fields = '__all__'
However, I am unable to get any 'fields' (getting None) key inside overrided get_fields_name method. I know of other ways as well like using StringRelatedField but that would change the output representation to
"genre":[
"Pop",
"Rock"
]
Whereas, I want to stick to my original representation
"genre": [
{
"id": 3,
"name": "Pop",
"created_date": "2018-09-05T17:05:59.705422+05:30",
"updated_date": "2018-09-20T14:43:02.062107+05:30",
"status": false
},
{
"id": 4,
"name": "Rock",
"created_date": "2018-09-05T17:06:06.889047+05:30",
"updated_date": "2018-09-17T16:45:22.684044+05:30",
"status": true
},
{
"id": 5,
"name": "Classical",
"created_date": "2018-09-05T17:06:14.216260+05:30",
"updated_date": "2018-09-05T17:06:14.275082+05:30",
"status": true
}
]
UPDATE - What I want is like this
"genre": [
{
"name": "Pop"
},
{
"name": "Rock"
},
{
"name": "Classical"
}
]
Contexts are meant to be set to the root serializer only.
Whenever UserSongSerializer will be instantiated it'll override the nested genre context.
If you are using generic views, you'll want to override the view's get_serializer_context and add your own context there. It's documented at the bottom of the methods section
PS: context are "shared" to serializers, fields, validators.
PPS: Don't alter context after it's been set you it's going to be sort of undefined behavior.
I'm having an issue in Django RestFramework in testing.
I have the following test:
def test_update_coupon(self):
response = self.make_coupon_request(
kind="put",
version="v1",
id=2,
data=self.valid_coupon_data
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
Where make_coupon_request has a return of:
return self.client.put(
reverse("coupon",
kwargs={
"version": kwargs["version"],
"pk": kwargs["id"]
}
),
data=json.dumps(kwargs["data"]),
content_type='application/json'
)
and valid_coupon_data where the problem is occurring is:
self.valid_coupon_data = {
"company": Company.objects.get(id=1),
"name": "Coupon Updated",
"added": "2018-11-30",
"code": "TESTCODE"
}
edit - An example Company that would be in this structure is:
{
"id": 1,
"name": "test",
"added": "2018-11-30"
},
So the total structure would look like:
self.valid_coupon_data = {
"company": {
"id": 1,
"name": "test",
"added": "2018-11-30"
},
"name": "Coupon Updated",
"added": "2018-11-30",
"code": "TESTCODE"
}
The error I am getting is in make_coupon_request that json.dumps cannot serialize valid_coupon_data:
"TypeError: Object of type Company is not JSON serializable"
I have a serializer for Company:
class CompanySerializer(serializers.ModelSerializer):
coupons = CouponSerializer(many=True, read_only=True)
class Meta:
model = Company
fields = ("name", "added", "coupons")
And for coupon:
class CouponSerializer(serializers.ModelSerializer):
class Meta:
model = Coupon
fields = ("company", "name", "added", "code")
Basically I know that somehow I need to use a serializer in order to make my test work, as json.dumps isn't accepting the raw Company object... but I am not sure how nor do I quite understand why.
Here are my 2 models for reference:
class Company(models.Model):
name = models.CharField(max_length=255, null=False)
class Meta:
verbose_name_plural = "Companies"
class Coupon(models.Model):
company = models.ForeignKey(
Company, on_delete=models.CASCADE, related_name='coupons')
name = models.CharField(max_length=100)
added = models.DateField(auto_now_add=True)
code = models.CharField(max_length=255, null=False)
The problem may lies in this statement,
self.valid_coupon_data = {
"company": Company.objects.get(id=1), # here
"name": "Coupon Updated",
"added": "2018-11-30",
"code": "TESTCODE"
}
It's clear that you are trying to send a json data and unfortunately it contains a non-serializable data of Company type.
According to the CouponSerializer you'd provided, the below json is enough to create/update the Coupon instance.
{
"company": 1, # provide only integer value
"name": "Coupon Updated",
"added": "2018-11-30",
"code": "TESTCODE"
}
The problem is that you are passing a python object via a json in your test, this section
self.valid_coupon_data = {
"company": Company.objects.get(id=1), # This is the error point! you are passing python object not a json.
"name": "Coupon Updated",
"added": "2018-11-30",
"code": "TESTCODE"
}
And passing just integer value like other answers will not work either. you should pass json of company object too. like this:
company = Company.objects.get(id=1)
self.valid_coupon_data = {
"company": {
"id": company.id,
"name": company.name,
"added": company.added
},
"name": "Coupon Updated",
"added": "2018-11-30",
"code": "TESTCODE"
}
Note
By the way if you are using django rest, then the way you are returning data in your views is not correct. use to_representation or serializer.data methods. it's not that well to use json.dump when you have a powerfull library like django-rest-framework. You can also return json as is with Response library of django-rest. like return Response(jsonData)
If you want more clear answer, please provide make_coupon_request method codes. to see what's going on in there.
Due to the way my database is designed, images are not stored with the project.
This is because there is no set amount of images per product. Some may have 1 image, others may have 10.
I would like my API to return content nested within itself. Currently, my code simply repeats the entire object when additional images exist for the item.
I am using Django Rest Framework:
class ProductDetailView(APIView):
renderer_classes = (JSONRenderer, )
def get(self, request, *args, **kwargs):
filters = {}
for key, value in request.GET.items():
key = key.lower()
if key in productdetailmatch:
lookup, val = productdetailmatch[key](value.lower())
filters[lookup] = val
qset = (
Product.objects
.filter(**filters)
.values('pk', 'brand')
.annotate(
image=F('variation__image__image'),
price=F('variation__price__price'),
name=F('variation__name'),
)
)
return Response(qset)
Currently, an item with 3 images pointing to it will look like this:
[{
"name": "Amplitiue jet black",
"brand": "Allup",
"price": "$1248",
"vari": "917439",
"image": "url1",
},
{
"name": "Amplitiue jet black",
"brand": "Allup",
"price": "$1248",
"vari": "917439",
"image": "url",
},
{
"name": "Amplitiue jet black",
"brand": "Allup",
"price": "$1248",
"vari": "917439",
"image": "url",
},
]
Ideally, it should look like this, combining all the images within an array:
{
"name": "Amplitiue jet black",
"brand": "Allup",
"price": "$1248",
"vari": "917439",
"images": [
"url1",
"url2"
"url3"
],
}
You should use a ListApiView together with a ModelSerializer. Don't put the filtering in the get method, the Django class based view way is to use get_queryset for that.
from rest_framework import serializers, generics
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = ("url",)
class ProductSerializer(serializers.ModelSerializer):
images = ImageSerializer(many=True)
class Meta:
model = Product
fields = ("name", "brand", "price", "vari", "images")
class ProductListView(generics.ListAPIView): # it is not a details view
serializer_class = ProductSerializer
def get_queryset(self):
filters = {}
for key, value in self.request.GET.items():
key = key.lower()
if key in productdetailmatch:
lookup, val = productdetailmatch[key](value.lower())
filters[lookup] = val
return Product.objects.prefetch_related("images").filter(**filters)
The image list in the JSON will be objects with one "url" element instead of just a list of urls, but this is more consistent with REST standards anyway.