I have double nested serializer situation...
i have three models : Reports, ReportPages and widgets , upon trying to create a specific endpoind that is :
payload {
"since_date": "some date",
"until_date": "some other date that is greater than since_date",
"report_pages": [
{
"page_number": "some number" (generated from front end, of type integer)
"widgets": [
{
"data": ["some array"],
"width": "some number",
"height": "some number",
"top_position": "some number",
"left_position": "some number",
"widget_type": "" (either "Label", "LineChart", "Bar" or "PieChart"),
}
]
}
]
}
I faced a problem with nested serializer, i was only able to create the first half which is :
payload {
"since_date": "some date",
"until_date": "some other date that is greater than since_date",
"report_pages": [
{
"page_number": "some number" (generated from front end, of type integer)
]
}
Knowing that the creation of a report is separate from this endpoint, so i override the update method and structure looks like this :
Report{
report_page{
widgets {}
}
}
My models :
class Reports(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=500, null=True)
since_date = models.DateTimeField(null=True)
until_date = models.DateTimeField(null=True)
def __str__(self):
return self.name
class ReportPages(models.Model):
id = models.AutoField(primary_key=True)
number = models.IntegerField(null=True)
report = models.ForeignKey(Reports, related_name="report_pages",on_delete=models.CASCADE)
def __str__(self):
return self.number
class Widgets(models.Model):
id = models.AutoField(primary_key=True, unique=True)
data = ArrayField(models.CharField(max_length=10, blank=True))
width = models.IntegerField(null=True)
height = models.IntegerField(null=True)
top_position = models.FloatField(null=True)
left_position = models.FloatField(null=True)
widget_type = models.CharField(max_length=500, null=True)
report_pages = models.ForeignKey(ReportPages,related_name="widgets", on_delete=models.CASCADE)
My views :
class Reports2ViewSet(viewsets.ModelViewSet):
queryset = Reports.objects.all()
serializer_class = Reports2Serializer
http_method_names = ['get','post','retrieve','put','patch']
My serializers :
class ReportWidgetsSerializer(serializers.ModelSerializer):
class Meta:
model = Widgets
fields = '__all__'
class ReportPagesSerializer(serializers.ModelSerializer):
widgets = ReportWidgetsSerializer(many=True)
class Meta:
model = ReportPages
fields = ('number','widgets',)
def update(self, instance, validated_data):
return super().update(instance, validated_data)
class Reports2Serializer(serializers.ModelSerializer):
report_pages = ReportPagesSerializer(many=True)
class Meta:
model = Reports
fields = ('report_pages','since_date','until_date',)
def update(self, instance, validated_data):
page_list = validated_data.pop('report_pages')
instance.since_date = validated_data.get('since_date', instance.since_date)
instance.until_date = validated_data.get('until_date', instance.until_date)
instance.save()
serializer = self.fields['report_pages']
widget_list = []
for page in page_list:
page['report'] = instance
widget_list = page.pop('widgets')
page = ReportPages.objects.create(**page)
print(page)
for widget in widget_list:
print('the widget is', widget)
widget['report_pages'] = page
widget = Widgets.objects.create(**widget)
print(widget)
# pages = serializer.create(page_list)
return instance
the error is :
{
"report_pages": [
{
"widgets": [
{
"report_pages": [
"This field is required."
]
}
]
},
{
"widgets": [
{
"report_pages": [
"This field is required."
]
}
]
}
]
}
Related
I`m trying to post this json object
{
"name": "Country Name",
"description": "Description here...",
"generalInfo": {
"capital": "Capital",
"population": "Population",
"highestPeak": "HighestPeak",
"area": "Area"
},
"timelineData": [
{
"name": "Name 1",
"ruler": "Ruler",
"dateStart": "dateStart",
"dateEnd": "dateEnd",
"description": "description"
},
{
"name": "Name 2",
"ruler": "Ruler",
"dateStart": "dateStart",
"dateEnd": "dateEnd",
"description": "description"
},
{
"name": "Name 3",
"ruler": "Ruler",
"dateStart": "dateStart",
"dateEnd": "dateEnd",
"description": "description"
}
]
}
But I`m receiving this error
ValueError at /countries/
Cannot assign "OrderedDict([('capital', 'Capital'), ('population', 'Population'), ('highestPeak', 'HighestPeak'), ('area', 'Area')])": "Country.generalInfo" must be a "GeneralInfo" instance.
Request Method: POST
Request URL: http://127.0.0.1:8000/countries/
Django Version: 4.0.3
Here are my models.py:
class GeneralInfo(models.Model):
capital = models.CharField(max_length=200)
population = models.CharField(max_length=200)
highestPeak = models.CharField(max_length=200)
area = models.CharField(max_length=200)
class TimelineData(models.Model):
name = models.CharField(max_length=200)
ruler = models.CharField(max_length=200)
dateStart = models.CharField(max_length=200)
dateEnd = models.CharField(max_length=200)
description = models.TextField(default='Description here...')
class Country(models.Model):
name = models.CharField(max_length=200, db_index=True)
description = models.TextField(default='Description here...')
generalInfo = models.ForeignKey(GeneralInfo, on_delete=models.CASCADE)
timelineData = models.ManyToManyField(TimelineData)
I have also overridden the create method for CountrySerializer and here are
my serializers.py:
class TimelineDataSerializer(serializers.ModelSerializer):
class Meta:
model = TimelineData
fields= '__all__'
class GeneralInfoSerializer(serializers.ModelSerializer):
class Meta:
model= GeneralInfo
fields = '__all__'
class CountrySerializer(serializers.ModelSerializer):
timelineData = TimelineDataSerializer(many=True)
generalInfo = GeneralInfoSerializer()
class Meta:
model= Country
fields = '__all__'
def create(self, validated_data):
timelineData = validated_data.pop('timelineData')
country = Country.objects.create(**validated_data)
for timelineItem in timelineData:
TimelineData.objects.create(country=country,**timelineItem)
return country
and views.py
class CountryList(viewsets.ModelViewSet):
serializer_class = CountrySerializer
queryset = Country.objects.all()
I am new to Django and Python so maybe I missed something. I've been stuck on this for a while now and wasn't able to find any solution myself
The error is because that OrderedDict needs to be an actual GeneralInfo Object..
I have not used serializers much (or at all tbh), but this would be my general guess from what I've seen from similar problems:
class CountrySerializer(serializers.ModelSerializer):
timelineData = TimelineDataSerializer(many=True)
generalInfo = GeneralInfoSerializer()
class Meta:
model= Country
fields = '__all__'
def create(self, validated_data):
timelineData = validated_data.pop('timelineData')
# pop out data (so it's not in final create), filter for object
generalInfoObj = GeneralInfo.objects.filter(**validated_data.pop('generalInfo')).first()
# ^ get or None (no crash, my preference)
# could also be:
# generalInfoObj = GeneralInfo.objects.get( ... )
# ^ - get or crash
# generalInfoObj, created = GeneralInfo.objects.get_or_create( ... )
# ^ get or create it
# debugger
print('generalInfoObj', type(generalInfoObj), generalInfoObj)
country = Country.objects.create(**validated_data, GeneralInfo=generalInfoObj)
for timelineItem in timelineData:
TimelineData.objects.create(country=country,**timelineItem)
return country
If generalInfo shows up like an array of tuples, you'd do something like this with List Comprehension
(adding this just in case)
generalInfoObj = GeneralInfo.objects.filter(**{i[0]:i[1] for i in validated_data.pop('generalInfo')}).first()
## breakdown:
# generalinfo = [('key0', val0'), ('key1', val1')]
dict = {}
for i in generalinfo:
# i = ('key', 'value')
dict[i[0]] = i[1]
# dict = {
# 'key0': 'val0',
# 'key1': 'val1',
# }
Please help me improve my code.. im newly placed and learning things
i want to display separately product field in my code just like category and sub-category in my code but i don't know how to. Also please point out if there anything that i can improve in my code
Here is my output:
[
{
"Category": {
"c_id": 1,
"parent_id": 15
},
"Sub_category": {
"sc_id": 1,
"is_active2": 1
},
"p_id": 2,
"group_id": "",
"product_name": "Fruit",
"is_prescription_needed": 1,
}]
Whereas my expected format is:
[
{
"Category": {
"c_id": 1,
"parent_id": 15
},
"Sub_category": {
"sc_id": 1,
"is_active2": 1
},
"Product": { # How do i separate this "Product" key and value
"p_id": 2,
"group_id": "",
"product_name": "Fruit",
"is_prescription_needed": 1
}]
here is my code:
Serialziers.py
class ProductCategorySerializer(serializers.ModelSerializer):
c_id = serializers.SerializerMethodField()
category_image = serializers.SerializerMethodField()
class Meta:
fields = ["c_id","parent_id","category","category_image","is_active"]
model = Category
def get_c_id(self,obj):
return obj.id
def get_category_image(self,obj):
return obj.image
class ProductSubCategorySerializer(serializers.ModelSerializer):
sc_id = serializers.SerializerMethodField()
is_active2 = serializers.SerializerMethodField()
class Meta:
fields = ["sc_id","sub_category","is_active2"]
model = ProductSubCategory
def get_sc_id(self,obj):
return obj.id
def get_is_active2(self,obj):
return obj.is_active
class ProductSerializer(serializers.ModelSerializer):
Category = ProductCategorySerializer(source='category', read_only=True)
Sub_category = ProductSubCategorySerializer(source='sub_category', read_only=True)
product_image = serializers.SerializerMethodField()
p_id = serializers.SerializerMethodField()
class Meta:
fields = ["Category","Sub_category", "p_id", "group_id", "product_name", "is_prescription_needed",
"product_description", "product_keypoint", "product_type", "product_image", "pack", "pack_unit", "hsn",
"company", "brand_id", "composition", "drug_details", "uses",
"side_effects", "cope_side_effects", "how_it_works", "safety_advice", "what_if_forget",
"medical_constraints", "created_at", "created_by", "modified_at", "modified_by"]
model = Product
def get_product_image(self, obj: Product):
image = obj.image
if len(image) == 0:
return ""
return image
def get_p_id(self,obj):
return obj.id
View.py
class categoryProduct(generics.GenericAPIView):
def post(self, request):
params = request.query_params
category = params.get("category")
sub_category = params.get("sub_category")
kwargs = {}
if category:
kwargs['category'] = category
else:
kwargs['category'] = 1
if sub_category:
kwargs['sub_category'] = sub_category
else:
kwargs['sub_category'] = 1
queryset = Product.objects.filter(**kwargs)
all_response = ProductSerializer(queryset, many=True)
#AFTER PAGINATION
return Response(all_response)
models.py
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, db_column='category')
sub_category = models.ForeignKey(ProductSubCategory, on_delete=models.CASCADE, db_column='sub_category')
group_id = models.CharField(max_length=200)
product_name = models.CharField(max_length=500)
is_prescription_needed = models.IntegerField()
...
class Meta:
managed = False
db_table = 'product'
class Category(models.Model):
category = models.CharField(max_length=50)
image = models.CharField(max_length=500)
store_image = models.CharField(max_length=1000)
is_active = models.IntegerField()
parent_id = models.IntegerField()
class Meta:
managed = False
db_table = 'category'
class ProductSubCategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE, db_column="category")
sub_category = models.CharField(max_length=250)
is_active = models.IntegerField()
class Meta:
managed = False
db_table = 'sub_category'
You can make a separate field that is a a SerializerField of a serializer you create with the proper fields you want displayed under the Product key. This is how your other field, like Category get appropriately created. That is to say: remove the fields from the ProductSerializer into a new serializer, then add a Product SerializerField with the new serializer containing those keys.
One other way you can do this is to create a .to_representation method to change the output to precisely the format you want.
class ProductSerializer(serializers.ModelSerializer):
...
def to_representation(self, instance):
representation = super().to_representation(instance)
representation['Product'] = {
"p_id": representation.pop('p_id')
"group_id": representation.pop('group_id'),
"product_name": representation.pop('product_name'),
"is_prescription_needed": representation.pop('is_prescription_needed')
}
return representation
I'm trying to format data when querying my API. I can retrieve my data like that :
"results": [
{
"Cat1": [
{
"job": String,
"position": Integer
}
]
},
{
"Cat1": [
{
"job": String,
"position": Integer
}
]
},
{
"Cat2": [
{
"job": String,
"position": Integer
}
]
}
]
But I want something like that:
"results": [
{
"Cat1": [
{
"job": String,
"position": Integer
},
{
"job": String,
"position": Integer
}
]
},
{
"Cat2": [
{
"job": String,
"position": Integer
}
]
}
]
I use a serializer like this:
class CustomSerializer(serializers.ModelSerializer):
category = CatSerializer()
job = JobSerializer()
class Meta:
model = MyModel
fields = '__all__'
def to_representation(self, value):
return {
value.category.name: [{"job": value.job.name,
"position": value.position, }]
cat1 and cat2 are dynamics, they are from another table. I don't understand how to create my arrays properly using those serializers. The category is a #Property field in my model who's a foreign key of job.
My models:
class MyModel(models.Model):
CHOICES = [(i, i) for i in range(4)]
partner = models.ForeignKey(Partner, on_delete=models.CASCADE)
job = models.ForeignKey(
Job, on_delete=models.CASCADE)
position = models.IntegerField(choices=CHOICES)
#property
def category(self):
return self.job.category.domain
def __str__(self):
return '%s | %s | %s | position: %s' % (self.partner.name, self.domain.name, self.job.name, self.position)
class Job(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
code = models.CharField(
max_length=255, unique=True)
name = models.CharField(
max_length=255)
class Category(models.Model):
domain = models.ForeignKey(Domain, on_delete=models.CASCADE)
code = models.CharField(
max_length=5)
name = models.CharField(max_length=255)
hourly_rate = models.FloatField(
null=True, blank=True)
How should I deal with serializers to format my data properly ?
EDIT:
I ended with something like that, except for the ListSerializer.
I used 2 ModelSerilizers
class MyModelCustomSerializer(serializers.ModelSerializer):
position = serializers.IntegerField(read_only=True)
job = serializers.CharField(source='job.name', read_only=True)
class Meta:
model = MyModel
fields = ['job', 'position']
def to_representation(self, value):
return {"position": value.position,
"job": {"name": value.job.name, "slug": value.job.slug,
"title": value.job.seo_title}
}
And
class CategoryCustomSerializer(serializers.ModelSerializer):
models = MyModelustomerSerializer(many=True)
class Meta:
model = Category
fields = ['category', 'MyModel']
def to_representation(self, value):
filters = {'job__category__domain__name': value.name}
myModels = MyModel.objects.filter(**filters)
serializer = MyModelCustomSerializer(instance=myModels, many=True,)
return {value.name: serializer.data}
But if I try to use a jobSerializer who already exist instead of
"job": {"name": value.job.name, "slug": value.job.slug,
"title": value.job.seo_title}
},
I got this error: Object of type 'Job' is not JSON serializable, but it's working anyway because i don't need all fields
I would go the direction of implementing a custom ListSerializer for the ModelSerializer and overriding its to_representation method.
from rest_framework import serializers
from collections import OrderedDict
class CustomListSerializer(serializers.ListSerializer):
def to_representation(self, data):
iterable = data.all() if isinstance(data, models.Manager) else data
list_rep = OrderedDict()
for item in iterable:
child_rep = self.child.to_representation(item)
k, v = list(child_rep.items()).pop()
list_rep.setdefault(k, []).append(v)
return [
{k: v}
for k, v in list_rep.items()
]
Then set the model Meta to use it
class CustomSerializer(serializers.ModelSerializer):
category = CatSerializer()
job = JobSerializer()
class Meta:
model = MyModel
fields = '__all__'
list_serializer_class = CustomListSerializer
def to_representation(self, value):
return {
value.category.name: [{"job": value.job.name,
"position": value.position, }]
I want to achieve this: a API to get all products for a category.
Models.py
class Category(models.Model):
main_category = models.CharField(max_length=200, default = '')
def __str__(self):
return '%s' % (self.main_category)
class Subcategory(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
sub_category = models.CharField(max_length=200, default = '')
def __str__(self):
return '%s' % (self.sub_category)
class Products(models.Model):
sub_category = models.ForeignKey(Subcategory, on_delete=models.CASCADE)
product_name = models.CharField(max_length=200, default = '')
def __str__(self):
return '%s' % (self.product_name)
Views.py
class ProductCategoryFilter(django_filters.FilterSet):
category = django_filters.CharFilter(name="category__main_category")
class Meta:
model = Products
fields = ['category']
class ProductCategoryListAPIView(ListAPIView):
queryset = Products.objects.all()
serializer_class = ProductCategorySerializer
filter_class = ProductCategoryFilter
Serializers.py
class ProductCategorySerializer(ModelSerializer):
#category_name = serializers.CharField(source='category.main_category')
category_name = CategorySerializer(source='category_set')
class Meta:
model = Products
fields = [
'category_name',
'product_name',
]
AttributeError at /api/productcategories/
I get AttributeError when attempting to get a value for field category_name on serializer ProductCategorySerializer. The serializer field might be named incorrectly and not match any attribute or key on the Products instance.
Original exception text was: 'Products' object has no attribute 'category'.
And I want the output like this:
[
{
"category_name": "main category 1",
"product_name": "product 1"
},
{
"category_name": "main category 1",
"product_name": "product 2"
},..........
OR output like this :
[
{
"main category 1" :{ "product 1", "product 2"...}
I try to serialize and save following json
{
"start": "2017-12-12",
"end": "2017-12-12",
"has_refund": false,
"room": {
"key": 0
},
"reserved_days": [ {
"date": "2017-12-12",
"price": "2",
"paymentMethod": "3"
},
{
"date": "2017-12-13",
"price": "2",
"paymentMethod": "3"
}
],
"check_in_time": "14:00",
"check_out_time": "12:00",
"guest_name": "Ivan",
"payed": false
}
SERIALIZERS
class DaySerializer(serializers.ModelSerializer):
class Meta:
model = Day
fields = [
'date',
'price',
'paymentMethod',
]
class RoomSerializer(serializers.ModelSerializer):
class Meta:
model = Room
fields = [
'key',
]
class ReservationSerializer(serializers.ModelSerializer):
room = RoomSerializer()
reserved_days = DaySerializer(many=True)
class Meta:
model = Reservation
fields = [
'start',
'end',
'check_in_time',
'check_out_time',
'reserved_days',
'room',
'has_refund',
'payed',
'guest_name',
'reservation_number',
]
def create(self, validated_data):
day_data = validated_data.pop('reserved_days')
room = validated_data.pop('room')
reservation = Reservation.objects.create(**validated_data)
existing_room = Room.objects.get(key=room['key'])
reservation.room = existing_room
reservation.save()
for day in day_data:
day, created = Day.objects.get_or_create(date=day['date'], price=day['price'], paymentMethod=day['paymentMethod'])
reservation.reserved_days.add(day)
return reservation
MODEL
class Day(models.Model):
date = models.DateField(auto_now=False, auto_now_add=False)
price = models.FloatField()
paymentMethod = models.CharField(max_length = 200)
def __unicode__(self):
return str(self.date)
class Room(models.Model):
name = models.CharField(max_length = 200, null=True)
key = models.AutoField(primary_key=True)
def __unicode__(self):
return self.name
class Reservation(models.Model):
start = models.DateField(verbose_name='Заезд', auto_now=False, auto_now_add=False, blank=False)
end = models.DateField(verbose_name='Выезд', auto_now=False, auto_now_add=False, blank=False)
check_in_time = models.TimeField(verbose_name='Время заезда', blank=False)
check_out_time = models.TimeField(verbose_name='Время выезда', blank=False)
has_refund = models.BooleanField(verbose_name='Возвратная бронь', default=True)
payed = models.BooleanField(verbose_name='Оплачено', default=False)
room = models.ForeignKey(Room, null=True, blank=True, verbose_name='Номер', on_delete=models.CASCADE)
reserved_days = models.ManyToManyField(Day, blank=False)
guest_name = models.CharField(verbose_name='Имя гостя', max_length=200, blank=True)
reservation_number = models.CharField(verbose_name='Номер брони', max_length=200, blank=True)
In response I get 500 error and this
KeyError at /core/create/
'key'
However, if I change field of room to name (inside room serializer and create method of reservation serializer), everything works perfectly
class RoomSerializer(serializers.ModelSerializer):
class Meta:
model = Room
fields = [
'name',
]
class ReservationSerializer(serializers.ModelSerializer):
room = RoomSerializer()
reserved_days = DaySerializer(many=True)
class Meta:
model = Reservation
fields = [
'start',
'end',
'check_in_time',
'check_out_time',
'reserved_days',
'room',
'has_refund',
'payed',
'guest_name',
'reservation_number',
]
def create(self, validated_data):
day_data = validated_data.pop('reserved_days')
room = validated_data.pop('room')
reservation = Reservation.objects.create(**validated_data)
existing_room = Room.objects.get(name=room['name'])
reservation.room = existing_room
reservation.save()
for day in day_data:
day, created = Day.objects.get_or_create(date=day['date'], price=day['price'], paymentMethod=day['paymentMethod'])
reservation.reserved_days.add(day)
return reservation
Seems that it works only with strings.
How to make it work with number ? The same thing happened with me when I tried saving with pk instead of separated int fieled