How to convert to a full JSON - python

How to convert to JSON type format.
Instead of the object ID you need to get full information about it.
From such
[
{
"title": "scenario factory - sump",
"scenario_pipeline": [
{
"pipeline": 11
}
],
"scenario_exist": [
{
"factory": 43
},
{
"factory": 44
}
]
}
]
To such a
[
{
"title": "scenario factory - sump",
"scenario_pipeline": [
{
"title": "factory - sump",
"percent": 11,
"start_point": [
57.332892983304895,
36.40013349999995
],
"end_point": [
51.829824506973154,
43.43138349999996
],
"point": []
}
],
"scenario_exist": [
{
"id": 43,
"title": "factory",
"choice": "Factory",
"address": [
57.332892983304895,
36.40013349999995
]
},
{
"id": 44,
"title": "sump",
"choice": "Sump",
"address": [
51.829824506973154,
43.43138349999996
]
}
]
}
]
My models.py
The basic model of Scenario, the two remaining inline
class Scenario(models.Model):
title = models.CharField(max_length=200)
class ScenarioExist(models.Model):
scenario = models.ForeignKey('Scenario', related_name='scenario_exist', on_delete=models.CASCADE)
factory = models.ForeignKey('factory.Factory', related_name='factory', on_delete=models.CASCADE)
class PipelineTwo(models.Model):
scenario = models.ForeignKey('Scenario', related_name='scenario_pipeline', on_delete=models.CASCADE)
pipeline = models.ForeignKey('pipeline.Pipeline', null = True, on_delete=models.CASCADE, related_name = 'point_two')
If you want to show the models to which the fields are accessed, I will show.
My serializes.py
class PipelineTwoSerializer(serializers.ModelSerializer):
class Meta:
model = PipelineTwo
fields = ['pipeline']
class ScenarioExistSerializer(serializers.ModelSerializer):
class Meta:
model = ScenarioExist
fields = ['factory']
class ScenarioSerializer(serializers.ModelSerializer):
scenario_pipeline = PipelineTwoSerializer(many=True)
scenario_exist = ScenarioExistSerializer(many=True)
class Meta:
model = Scenario
fields = ['title', 'scenario_pipeline', 'scenario_exist']

You are using nested serializers in your ScenarioSerializer; you need to do the same in your PipelineTwoSerializer to get the values from pipeline.Pipeline and in ScenarioExistSerializer for factory.Factory.

Related

i have one model which having foreign key with images feild and in that i have multiple images can i get them in single list like this :

Models.py
class Cat_Breed_Detail(models.Model):
id = models.AutoField(primary_key=True,verbose_name="id")
key_name = models.CharField(max_length=35,default='',null=True,verbose_name="key_name")
display_name= models.CharField(max_length=35,default='',null=True,verbose_name="display_name")
def __str__(self):
return self.key_name
class Cat_Image(models.Model):
image=models.ImageField(upload_to="ImagesCat/")
cat=models.ForeignKey(Cat_Breed_Detail,on_delete=models.CASCADE,null=True,related_name='images')
def image_preview(self):
if self.image:
return mark_safe('<img src="{0}" width="150" height="150" />'.format(self.image.url))
else:
return '(No image)'
Selializer.py
class CatImageSerializer(serializers.ModelSerializer):
class Meta:
model = Cat_Image
fields = '__all__'
class CatDataSerializer(serializers.ModelSerializer):
class Meta:
model = Cat_Breed_Detail
fields = ['id','key_name','display_name','images']
View.py
class CatListCreate(ListAPIView):
queryset = CatBreedDetails.objects.all().order_by('key_name')
serializer_class = CatDataSerializer
pagination_class = CustomPagination
response i am getting here i am getting nested response i dont want it i just want image url in images as a list not like the response i am currently getting :
{
"id": 9,
"key_name": "abyssinian",
"display_name": "Abyssinian",
"image": [
{
"id": 3,
"image": "/media/Abyssinian_0006.jpg",
"cat": 9
},
{
"id": 4,
"image": "/media/Abyssinian_0092.jpg",
"cat": 9
}
]
},
{
"id": 18,
"key_name": "american_bobtail",
"display_name": "American Bobtail",
"image": [
{
"id": 5,
"image": "/media/American_Bobtail_0004.jpg",
"cat": 18
},
{
"id": 6,
"image": "/media/American_Bobtail_0057.png",
"cat": 18
}
]
},
{
"id": 19,
"key_name": "american_curl",
"display_name": "American Curl",
"image": [
{
"id": 7,
"image": "/media/American_Curl_0078.jpg",
"cat": 19
},
{
"id": 8,
"image": "/media/American_Curl_0083.png",
"cat": 19
}
]
}
i want to get all the images in one list not like image:{image:"...."
,image:"......"} i am new to djnago and still learning is there any way to make it done .
is there any way i can get response like that :
{
"id": 9,
"key_name": "abyssinian",
"display_name": "Abyssinian",
"image": ["/media/Abyssinian_0006.jpg","/media/Abyssinian_0092.jpg",]
}
[
{
"id": 1,
"key_name": "first cat",
"display_name": "first cat",
"images": [
{
"image": "http://localhost:8000/ImagesCat/Screenshot_from_2022-12-21_18-16-28.png"
},
{
"image": "http://localhost:8000/ImagesCat/Screenshot_from_2022-12-29_13-41-31.png"
}
]
}
]
this is how I get response if you want to make like yours then you have to override list method
from rest_framework import serializers
from app.models import Cat_Image, Cat_Breed_Detail
class CatImageSerializer(serializers.ModelSerializer):
class Meta:
model = Cat_Image
fields = '__all__'
class CatImageSerializer2(serializers.ModelSerializer):
class Meta:
model = Cat_Image
fields = ['image']
class CatDataSerializer(serializers.ModelSerializer):
images = CatImageSerializer2(read_only=True, many=True)
class Meta:
model = Cat_Breed_Detail
fields = ['id', 'key_name', 'display_name', 'images']

How to GET multipule ModelSrializer in one APIVIEW using Django Rest Framework

I have UserModel each user has multiple package and each package have price and program.
model.py:
class User(models.Model):
name= models.CharField(max_length=100)
class Package(models.Model):
package_name = models.CharField(max_length=100)
user= models.ForeignKey(User, on_delete=models.CASCADE,related_name='user_package')
class Program(models.Model):
title = models.CharField(max_length=100)
user_package = models.ForeignKey(Package, on_delete=models.CASCADE)
class Price(models.Model):
price = models.FloatField()
user_package = models.ForeignKey(Package, on_delete=models.CASCADE)
serializer look like:
class UserSerializer(NestedCreateMixin, NestedUpdateMixin,serializers.ModelSerializer):
program = ProgramSerializer(source='user_program', many=True, read_only=True)
package = PackageSerializer(source='user_package', many=True, read_only=True)
price = PriceSerializer(source='price', many=True, read_only=True)
class Meta:
model = User
fields= ("__all__")
views.py:
class user_apiView(APIView):
def get(self, request):
user= user.objects.all()
serializer = UserSerializer(user, many = True)
return Response(serializer.data)
and that what I get:
{
"id": 1,
"package": [
{
"id": 1,
"package_name": "wfe",
"user": 1
},
{
"id": 2,
"package_name": "wfe",
"user": 1
}
]
}
how can GET this RESULT?
{
"id": 1,
"package": [
{
"id": 1,
"package_name": "wfe",
"user": 1
},
{
"id": 2,
"package_name": "wfe",
"user": 1
}
],
"price": [
{
"id": 1,
"price": "wfe",
"package": 1
},
{
"id": 2,
"price": "wfe",
"package": 2
}
]
"program": [
{
"id": 1,
"title": "wfe",
"package": 1
},
{
"id": 2,
"title": "wfe",
"package": 2
}
]
}
The problem is that your price and program models are not directly related to your user model. If you consider the relations it is like price -> package -> user, so you will have to get those relations in the package serializer instead like this
serializers.py
class ProgramSerializer(serializers.ModelSerializer):
class Meta:
model = Program
fields= ("__all__")
class PriceSerializer(serializers.ModelSerializer):
class Meta:
model = Price
fields= ("__all__")
class PackageSerializer(serializers.ModelSerializer):
program = ProgramSerializer(source='program_set', many=True, read_only=True)
price = PriceSerializer(source='price_set', many=True, read_only=True)
class Meta:
model = Package
fields= ("__all__")
class UserSerializer(serializers.ModelSerializer):
package = PackageSerializer(source='user_package', many=True, read_only=True)
class Meta:
model = User
fields= ("__all__")
Note that this will however not give you the output in the format you mentioned though, the price and program fields will instead be nested under package.
[
{
"id": 1,
"package": [
{
"id": 1,
"program": [
{
"id": 1,
"title": "program1",
"user_package": 1
}
],
"price": [
{
"id": 1,
"price": 1000.0,
"user_package": 1
}
],
"package_name": "package1",
"user": 1
},
{
"id": 2,
"program": [
{
"id": 2,
"title": "program2",
"user_package": 2
}
],
"price": [
{
"id": 2,
"price": 1500.0,
"user_package": 2
}
],
"package_name": "package2",
"user": 1
}
],
"name": "user1"
}
]
If you really want the format in the way you posted you might instead want to take a look at https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield

ManyToManyField value in Django REST Framework

So when I am routing to api/ to view my models I am not seeing what I expected to see.
I wanted to see the names of the strats that I grouped in Strat_Basket model in the variable basket but instead I see their ids which is generated by DJANGO automatically.
I want to see the names rather than numbers in the strat_basket view. It is more informative that's why.
models.py
class Strats(models.Model):
name = models.CharField(max_length=64)
class Strats_Basket(models.Model):
name = models.CharField(max_length=64)
basket = models.ManyToManyField(Strats, blank=True, related_name='list')
serializers.py:
class StratsSerializer(serializers.ModelSerializer):
class Meta:
model = Strats
fields = ('id', 'name')
class Strats_BasketSerializer(serializers.ModelSerializer):
class Meta:
model = Strats_Basket
fields = ('id', 'name', 'basket')
views.py:
class StratsView(viewsets.ModelViewSet):
serializer_class = StratsSerializer
queryset = Strats.objects.all()
class Strats_BasketView(viewsets.ModelViewSet):
serializer_class = Strats_BasketSerializer
queryset = Strats_Basket.objects.all()
urls.py:
router = routers.DefaultRouter()
router.register(r'strats', views.StratsView, 'strats')
router.register(r'strats_basket', views.Strats_BasketView, 'strats_basket')
API OUTPUT:
strats:
[
{
"id": 1,
"name": "strat1"
},
{
"id": 2,
"name": "strat2"
},
{
"id": 3,
"name": "strat3"
},
{
"id": 4,
"name": "strat4"
},
{
"id": 5,
"name": "strat5"
},
{
"id": 6,
"name": "strat6"
}
]
strats_basket:
Instead of 1,2,4 I want to see strat1, strat2, strat4.
[
{
"id": 1,
"name": "Basket 1",
"basket": [
1,
2,
4
]
},
{
"id": 2,
"name": "Basket 2",
"basket": [
3,
5,
6
]
}
]
You can use SerializerMethodField and inside the method return names list using values_list with flat=True
class Strats_BasketSerializer(serializers.ModelSerializer):
basket = serializers.SerializerMethodField()
def get_basket(self, obj):
return obj.basket.all().values_list('name', flat=True)
class Meta:
model = Strats_Basket
fields = ('id', 'name', 'basket')

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 full related objects in list

I'm rather new to Django Rest Framework and I'm trying to use DRF to to serialize a list of (related) objects.
I have the following models:
class Answer(models.Model):
value = models.CharField(max_length=128)
class User(models.Model):
name = models.CharField(max_length=128)
age = models.PositiveIntegerField()
class UserAnswer(models.Model):
user = models.ForeignKey(User)
answer = models.ForeignKey(Answer)
And the result I'm trying to get is in this form:
[
{
"name": "myName1",
"answers": [
{
"value": "myFirstAnswer"
},
{
"value": "mySecondAnswer"
},
{
"value": "myThirdAnswer"
},
]
},
{
"name": "myName2",
"answers": [
{
"value": "myFirstAnswer"
},
{
"value": "mySecondAnswer"
},
{
"value": "myThirdAnswer"
},
]
}
]
I'm trying to do it this way now:
class UserAnswerSerializer(serializers.ModelSerializer):
answers = AllUserAnswersSerializer(many=True, read_only=True)
class Meta:
model = User
fields = ('name', 'answers')
But then I get the following result:
[
{
"name": "myName1"
},
{
"name": "myName2"
}
]
And when I try to do it this way:
class UserAnswerSerializer(serializers.ModelSerializer):
answers = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
model = User
fields = ('name', 'answers')
Then i get the following result (an example again):
[
{
"name": "myName1",
"answers": [
1,
2,
3
]
},
{
"name": "myName2",
"answers": [
4,
5,
6
]
}
]
I'm having a hard time making this work, hope someone can show me how to convert the Primary Key's to actual objects!
Thanks!
Remove the explicit definition of the answers field in your serializer and add depth=1. It should look like this:
class UserAnswerSerializer(serializers.ModelSerializer):
class Meta:
depth = 1
model = User
fields = ('name', 'answers')
Info about depth: http://www.django-rest-framework.org/api-guide/serializers/#specifying-nested-serialization

Categories