how to create multiple objects with one request DRF - python

I have the following models
class Product(models.Model):
name = models.CharField(null=True, blank=True, max_length=500)
category = models.CharField(null=True, blank=True, max_length=120)
class SpecificationName(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE, null=True, related_name='specifications')
name = models.CharField(max_length=125)
class Attribute(models.Model):
spec_name = models.ForeignKey(SpecificationName, on_delete=models.CASCADE, null=True, related_name='attributes')
index = models.CharField(max_length=200, blank=True, null=True)
value = models.CharField(max_length=250, blank=True, null=True)
after saving objects in Django admin I have an example
{
"name": "Apple Smart Watch",
"category": "IT",
"specifications": [
{
"name": "Test Data",
"attributes": [
{
"index": "test",
"value": "test2"
},
{
"index": "test7",
"value": "test8"
},
{
"index": "test9",
"value": "test10"
}
]
},
{
"name": "Test Data Continued",
"attributes": [
{
"index": "bla",
"value": "bla1"
},
{
"index": "bla 2",
"value": "bla 4"
},
{
"index": "test9",
"value": "test10"
}
]
},
{
"name": "Test Spec",
"attributes": []
}
]
}
I need to save this kind of object with one request but I am failing to do this
my serializer looks like this
class ProductSerializer(serializers.ModelSerializer):
specifications = SpecNameSerializer(many=True)
# attributes = AttributeSerializer(many=True, required=False)
class Meta:
model = Product
fields = ['name', 'category', 'brand', 'price', 'specifications']
def create(self, validated_data):
specs = validated_data.pop('specifications')
instance = Product.objects.create(**validated_data)
for spec in specs:
SpecificationName.objects.create(product=instance, **spec)
print(spec)
return instance
with this code, I am getting the following result but not as expected
{
"name": "Appel watch series",
"specifications": [
{
"name": "Test Data",
"attributes": []
},
{
"name": "Test Data comn",
"attributes": []
},
{
"name": "Test Spec",
"attributes": []
}
]
}
it cannot write into attributes
I searched for many answers but I did not find or applied some of them, again it did not help me. I am using just ListCreateView in the views. Please is there anybody who can help solve this problem. Thanks in advance!

SOLVED
I am using here ModelSerializer instead I used Serializer and added some changes here my answer and it worked
class AttributeSerializer(serializers.Serializer):
index = serializers.CharField(max_length=200)
value = serializers.CharField(max_length=200)
class SpecNameSerializer(serializers.ModelSerializer):
attributes = AttributeSerializer(many=True)
class Meta:
model = SpecificationName
fields = '__all__'
class ProductSerializer(serializers.ModelSerializer):
specifications = SpecNameSerializer(many=True)
class Meta:
model = Product
fields = ['name', 'category', 'brand', 'price', 'specifications']
def create(self, validated_data):
specs = validated_data.pop('specifications')
instance = Product.objects.create(**validated_data)
for spec in specs:
SpecificationName.objects.create(product=instance, **spec)
attrs = spec.pop('attributes')
for attr in attrs:
Attribute.objects.create(spec_name=spec, **attr)
return instance

Related

how join 3 table in Django Python

This is my first class
class dot_bay(models.Model):
ma_dot_bay = models.CharField(primary_key=True,max_length=255, default=uuid.uuid4, editable=False)
ten_dot_bay = models.CharField(max_length=255, default="")
ngay_bay = models.DateTimeField()```
This is my second class
class video(models.Model):
ma_video = models.CharField(primary_key=True, max_length=255, default=uuid.uuid4, editable=False)
ma_dot_bay = models.ForeignKey(dot_bay, on_delete=models.CASCADE, related_name='dot_bay_video')
video_path = models.TextField()
detected_path = models.TextField()
ngay_upload = models.TextField()
And my third class
class hinh_anh(models.Model):
ma_hinh_anh = models.CharField(primary_key=True, max_length=255, default=uuid.uuid4, editable=False)
ma_video = models.ForeignKey(video, on_delete=models.CASCADE, related_name='video_hinh_anh')
image_base64 = models.TextField()
I try this in my Serializer in my project to display result of 2 join table dot_bay and video
like that
class DotBayModelSerializer(serializers.ModelSerializer):
class Meta:
model = dot_bay
fields = ("ma_dot_bay", "ten_dot_bay", "ngay_bay", "dot_bay_video")
depth = 1
And get the result like that
[
{
"ma_dot_bay": "db0001",
"ten_dot_bay": "Đợt bay",
"ngay_bay": "2021-05-14T15:30:27Z",
"dot_bay_video": [
{
"ma_video": "vd0001",
"video_path": "1",
"detected_path": "1",
"ngay_upload": "1",
"ma_dot_bay": "db0001"
},
{
"ma_video": "vd0002",
"video_path": "1",
"detected_path": "1",
"ngay_upload": "1",
"ma_dot_bay": "db0001"
}
]
},
{
"ma_dot_bay": "db0002",
"ten_dot_bay": "Đợt bay",
"ngay_bay": "2021-05-14T15:31:07Z",
"dot_bay_video": [
{
"ma_video": "vd0003",
"video_path": "1",
"detected_path": "1",
"ngay_upload": "1",
"ma_dot_bay": "db0002"
},
{
"ma_video": "vd0004",
"video_path": "11",
"detected_path": "1",
"ngay_upload": "1",
"ma_dot_bay": "db0002"
}
]
}
]
that's what I expected
But now I want to join 3 table, display like that,
[
{
"ma_dot_bay": "db0002",
"ten_dot_bay": "Đợt bay",
"ngay_bay": "2021-05-14T15:31:07Z",
"dot_bay_video": [
{
"ma_video": "vd0003",
"video_path": "1",
"detected_path": "1",
"ngay_upload": "1",
"ma_dot_bay": "db0002",
"video_hinh_anh": [
{
"ma_hinh_anh": "....."
},
{
"ma_hinh_anh": "....."
}
]
},
{
"ma_video": "vd0004",
"video_path": "11",
"detected_path": "1",
"ngay_upload": "1",
"ma_dot_bay": "db0002",
"video_hinh_anh": [
{
"ma_hinh_anh": "....."
},
{
"ma_hinh_anh": "....."
}
]
}
]
}
]
I try some method but not working :((((
How can I do that ??
The idea here is to use nested serializers. In our case, we'll use those nested serializers to detail how we want foreign key objects to be rendered/structure.
Try something like that:
class NestedHinhAnhModelSerializer(serializers.ModelSerializer):
class Meta:
model = hinh_anh
# Didnt put the `ma_video` field since we're using this serializer inside a video model
fields = ("ma_hinh_anh ", "image_base_64")
class NestedVideoModelSerializer(serializers.ModelSerializer):
# We override the `video_hinh_anh` field with a custom serializer
video_hinh_anh = NestedHinhAnhModelSerializer(many=True)
class Meta:
model = video
# I didnt put the `ma_dot_bay` fk field since we'll be using this serializer inside the dot_bay model
fields = ("ma_video ", "video_path ", "detected_path ", "ngay_upload", "video_hinh_anh")
class NestedVideoModelSerializer(serializers.ModelSerializer):
# We override the `dot_bay_video` with a custom serializer
dot_bay_video = VideoModelSerializer(many=True)
class Meta:
model = dot_bay
fields = ("ma_dot_bay", "ten_dot_bay", "ngay_bay", "dot_bay_video")

Django Rest Framework relationship queries in nested serializer

I am having difficulties in implementing nested serializers in Django REST Framework.
I'm building an Online score board, for which I currently have three models and I'm trying to serialize it into a single response.
models.py
class Player(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
def __str__(self):
return self.first_name
class Event(models.Model):
user = models.ManyToManyField("Player")
name = models.CharField(max_length=50)
desc = models.CharField(max_length=225)
def __str__(self):
return self.name
class LeadBoard(models.Model):
"""model for LeadBoard"""
player = models.ForeignKey(Player, on_delete=models.CASCADE, related_name='leadboard_player', null=True, blank=True)
event = models.ForeignKey(Event, on_delete=models.CASCADE, related_name='leadboard_event', null=True, blank=True)
score = models.IntegerField()
def __str__(self):
return self.score
The event model represent some kind of sports events and each Event can have multiple Players (user filed in Event model), the LeadBoard model stores the score of players in each event.
serializer.py
class PlayerSerializer(serializers.ModelSerializer):
class Meta:
model = Player
fields = ('id','first_name', 'last_name', 'bio')
class EventSerializer(serializers.ModelSerializer):
user = PlayerSerializer(read_only=True, many=True)
class Meta:
model = Event
fields = ('id', 'name', 'user', 'desc')
class LeadBoardSerializer(serializers.ModelSerializer):
event = EventSerializer(read_only=True)
class Meta:
model = LeadBoard
fields = ('id', 'event', 'score')
I have added two Players
[
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe"
}
]
and their scores in LeadBoard model
[
{
"id": 1,
"player": 1,
"event": 1,
"score": 20
},
{
"id": 2,
"player": 3,
"event": 1,
"score": 90
}
]
This is the response of LeadBoard,
[
{
"id": 1,
"event": {
"id": 1,
"name": "event2020",
"user": [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe"
}
],
"desc": "event description"
},
"score": 20
},
{
"id": 2,
"event": {
"id": 1,
"name": "event2020",
"user": [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe"
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe"
}
],
"desc": "event description"
},
"score": 90
}
]
But what I'm expecting to get is a response like this, which returns the Players(users) of events and their scores correctly.
[
{
"id": 1,
"event": {
"id": 1,
"name": "event2020",
"user": [
{
"id": 1,
"first_name": "Jhon",
"last_name": "Doe",
"score": 20
},
{
"id": 3,
"first_name": "Anna",
"last_name": "Doe",
"score": 90
}
],
"desc": "event description"
}
}
]
What am I missing here?
I'm new to Django and Django Rest Framework.
You need make response by event not by leadboard.
class PlayerSerializer(serializers.ModelSerializer):
class Meta:
model = Player
fields = ('id','first_name', 'last_name', 'bio')
class LeadBoardSerializer(serializers.ModelSerializer):
player = PlayerSerializer(read_only=True)
class Meta:
model = LeadBoard
fields = ('id', 'player', 'score')
class EventSerializer(serializers.ModelSerializer):
leadboard_event = LeadBoardSerializer(read_only=True, many=True)
class Meta:
model = Event
fields = ('id', 'name', 'desc', 'leadboard_event')
now use view to get event list
Update 1
if you want get score in player.
class PlayerSerializer(serializers.Serializer):
player_id = serializers.IntegerField()
first_name = serializers.CharField(max_length=256)
last_name = serializers.CharField(max_length=256)
score = serializers.IntegerField()
class LeadBoardSerializer(serializers.ModelSerializer):
player = serializers.SerializerMethodField()
class Meta:
model = LeadBoard
fields = ('id', 'player')
def get_player(self,obj):
player_dict = {'player_id': obj.player.id, 'first_name': obj.player.first_name, 'last_name': obj.player.last_name, 'score': obj.score}
result = PlayerSerializer(data=player_dict)
return result
class EventSerializer(serializers.ModelSerializer):
leadboard_event = LeadBoardSerializer(read_only=True, many=True)
class Meta:
model = Event
fields = ('id', 'name', 'desc', 'leadboard_event')
Try it.

RelatedObjectDoesNotExist when nesting data in DRF

I'm trying to serialize a model with nested data for its relationships. The data is returned properly in a GET request but when trying to POST data the following error is returned: api.models.Narration.narrative.RelatedObjectDoesNotExist: Narration has no narrative. I have no interest in POSTing nested data, it only needs to be returned in the GET requests and PKs can be used everywhere else. I have tried to using Meta.depth as well as defining the fields manually, both of which return the same exception. Code is below, any help is appreciated.
# serializers.py
class NarrationSerializer(ModelSerializer):
narrative = NarrativeSerializer(read_only=True)
settings = MapSettingsSerializer(read_only=True)
attached_events = CachedDataSerializer(many=True, read_only=True)
class Meta:
model = Narration
fields = "__all__"
# depth = 1 [also doesn't work]
# models.py
class Narration(OrderedModel):
narrative = models.ForeignKey(Narrative, on_delete=models.CASCADE)
title = models.TextField()
description = models.TextField()
date_label = models.CharField(max_length=100)
map_datetime = models.DateTimeField()
attached_events = models.ManyToManyField(CachedData)
img = models.URLField(blank=True, null=True)
video = models.URLField(blank=True, null=True)
settings = models.ForeignKey(MapSettings, on_delete=models.CASCADE)
order_with_respect_to = "narrative"
# views.py
class NarrationViewSet(viewsets.ModelViewSet):
queryset = Narration.objects.all()
serializer_class = NarrationSerializer
POSTed nested data (using PKs results in same exception):
{
"title": "cvxcv",
"description": "cxvxcv",
"date_label": "test",
"map_datetime": "0002-01-01T00:00:00Z",
"img": "",
"video": "",
"narrative": {
"author": "Test Author",
"title": "Test Narrative",
"description": "This is a test narrative.",
"tags": [
"test",
"tags"
]
},
"settings": {
"bbox": {
"type": "MultiPoint",
"coordinates": [
[
0,
0
],
[
1,
1
]
]
},
"zoom_min": 1,
"zoom_max": 12
},
"attached_events": [
{
"event_type": 178561,
"wikidata_id": 1,
"location": {
"type": "Point",
"coordinates": [
0,
0
]
},
"date": "0001-01-01",
"rank": 830700
}
]
}

Django Rest Framework: Get related specific model field in serializer from another model

I'm trying to return a Response including data from 2 linked models, where I can get a specific field value from another model. The models are:
class Author(models.Model):
author = models.CharField(max_length=200)
author_created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('author_created',)
class Book(models.Model):
book_name = models.CharField(max_length=200)
author_name = models.ForeignKey(Author, on_delete=models.CASCADE)
book_created = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ('book_created',)
Serializers are:
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = ('id', 'author')
class BookSerializer(serializers.ModelSerializer):
name = AuthorSerializer(source='author_name')
class Meta:
model = Book
fields = ('id', 'book_name','name')
My response shows like this:
[
{
"id": 1,
"book_name": "Himu",
"name": {
"id": 1,
"author": "Humayun Ahmed"
}
},
{
"id": 2,
"book_name": "Shanta's family",
"name": {
"id": 2,
"author": "Jafar Iqbal"
}
}
]
But i want to show it like this:
[
{
"id": 1,
"book_name": "Himu",
"name": {
"author": "Humayun Ahmed"
}
},
{
"id": 2,
"book_name": "Shanta's family",
"name": {
"author": "Jafar Iqbal"
}
}
]
How can i get this output?
Try something like this name = AuthorSerializer(source='author_name.author')
Reference : http://www.django-rest-framework.org/api-guide/fields/#source

django-tastypie : Related data not saving

My models.py
class Orders(models.Model):
order_id = models.CharField(max_length=7, primary_key=True)
users = models.ForeignKey(ProductUsers, on_delete=models.DO_NOTHING)
address = models.ForeignKey(ProductUsersAddress, on_delete=models.DO_NOTHING)
payment_method = models.CharField(default='COD', max_length=20, choices=PAYMENT_METHOD)
class OrderedProduct(models.Model):
products = models.ForeignKey(Products, on_delete=models.DO_NOTHING)
orders = models.ForeignKey(Orders, on_delete=models.CASCADE)
quantity = models.IntegerField(default=0)
price = models.DecimalField(default=0.00, max_digits=5, decimal_places=2, blank=False)
My resources.py
class OrdersResource(ModelResource):
ordered_products = fields.ToManyField('orders.resources.OrderedProductResource',
attribute=lambda bundle: OrderedProduct.objects.filter(orders=bundle.obj),
related_name='orders', full=True, null=True)
contact_no = fields.ForeignKey(ProductUsersResource, 'users')
address = fields.ForeignKey(ProductUsersAddressResource, 'address')
class Meta:
queryset = Orders.objects.all()
resource_name = 'orders'
include_resource_uri = False
collection_name = 'orders'
allowed_methods = ['get', 'post']
always_return_data = True
class OrderedProductResource(ModelResource):
products = fields.ForeignKey(ProductsResource, 'products')
orders = fields.ForeignKey(OrdersResource, 'orders')
class Meta:
queryset = OrderedProduct.objects.all()
resource_name = 'ordered_products'
excludes = ['id']
include_resource_uri = False
I entered data using Django-Admin.
When I hit, http://localhost:8000/orders/, I get,
{
"orders": [
{
"address": "/api/v1/address/1",
"contact_no": "/api/v1/users/8269661606",
"order_id": "KJLSWI",
"ordered_products": [
{
"orders": "/api/v1/orders/KJLSWI",
"price": "40.00",
"products": "/api/v1/products/1",
"quantity": 2
},
{
"orders": "/api/v1/orders/KJLSWI",
"price": "70.00",
"products": "/api/v1/products/2",
"quantity": 4
},
{
"orders": "/api/v1/orders/KJLSWI",
"price": "67.00",
"products": "/api/v1/products/3",
"quantity": 7
}
],
"payment_method": "COD",
}
]
}
Now according to tasty documentation,
Tastypie encourages “round-trippable” data, which means the data you
can GET should be able to be POST/PUT’d back to recreate the same
object.
If you’re ever in question about what you should send, do a GET on
another object & see what Tastypie thinks it should look like.
But when I post same data by just changing primary-key,
{
"address": "/api/v1/address/1",
"contact_no": "/api/v1/users/8269661606",
"order_id": "ABCDE",
"ordered_products": [
{
"orders": "/api/v1/orders/ABCDE",
"price": "40.00",
"products": "/api/v1/products/1",
"quantity": 2
},
{
"orders": "/api/v1/orders/ABCDE",
"price": "70.00",
"products": "/api/v1/products/2",
"quantity": 4
},
{
"orders": "/api/v1/orders/ABCDE",
"price": "67.00",
"products": "/api/v1/products/3",
"quantity": 7
}
],
"payment_method": "COD",
}
I get response,
{
"address": "/api/v1/address/1",
"contact_no": "/api/v1/users/8269661606",
"ordered_products": [],
"payment_method": "COD",
}
My data in model OrderedProduct, is not getting saved. WHYYYYYYY ??????
Try this:
def hydrate_m2m(self, bundle):
for ordered_product in bundle.data['ordered_products']:
if isinstance(ordered_product, dict):
ordered_product.update({'orders': bundle.obj})
return super(OrdersResource, self).hydrate_m2m(bundle)
and remove orders key from ordered_products JSON
and replace attribute=lambda bundle: OrderedProduct.objects.filter(orders=bundle.obj) with orders
and move related_name='orders' from resource to model.
Finally:
model:
class OrderedProduct(models.Model):
products = models.ForeignKey(Products, on_delete=models.DO_NOTHING)
orders = models.ForeignKey(Orders, on_delete=models.CASCADE, related_name='orders')
quantity = models.IntegerField(default=0)
price = models.DecimalField(default=0.00, max_digits=5, decimal_places=2, blank=False)
resource:
class OrdersResource(ModelResource):
ordered_products = fields.ToManyField('orders.resources.OrderedProductResource',
'orders', full=True, null=True)
contact_no = fields.ForeignKey(ProductUsersResource, 'users')
address = fields.ForeignKey(ProductUsersAddressResource, 'address')
class Meta:
queryset = Orders.objects.all()
resource_name = 'orders'
include_resource_uri = False
collection_name = 'orders'
allowed_methods = ['get', 'post']
always_return_data = True
def hydrate_m2m(self, bundle):
for ordered_product in bundle.data['ordered_products']:
if isinstance(ordered_product, dict):
ordered_product.update({'orders': bundle.obj})
return super(OrdersResource, self).hydrate_m2m(bundle)
and POST data:
{
"address": "/api/v1/address/1",
"contact_no": "/api/v1/users/8269661606",
"order_id": "ABCDE",
"ordered_products": [
{
"price": "40.00",
"products": "/api/v1/products/1",
"quantity": 2
},
{
"price": "70.00",
"products": "/api/v1/products/2",
"quantity": 4
},
{
"price": "67.00",
"products": "/api/v1/products/3",
"quantity": 7
}
],
"payment_method": "COD",
}

Categories