Assistance needed Django Rest Framework Serializer - python

I am new to the Django Rest Framework. I am attempting to get the navSidebarMaps object to serialize as follows:
[
"navSidebarName": "mainSidebar",
[
{
"id": 1,
"name": "item1",
"order": 1,
},
{
"id": 2,
"name": "item2",
"order": 2,
}
]
]
What is the recommended approach for pulling the list of navSidebarItems through to the navSidebarMaps? The related_name argument seems to get me part way to the goal. However, I've missed something.
Models:
class navSidebars(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
description = models.CharField(max_length=150, blank=False, null=False)
class navSidebarItems(models.Model):
name = models.CharField(max_length=50, blank=False, null=False)
description = models.CharField(max_length=150, blank=False, null=False)
class navSidebarMaps(models.Model):
fk_navsidebar = models.ForeignKey('core.navSidebars', related_name='sidebars', on_delete=models.CASCADE)
fk_navsidebar_items = models.ForeignKey('core.navSidebarItems', related_name='sidebar_items', on_delete=models.CASCADE)
order = models.IntegerField(default=0, blank=False, null=False)
Serializers:
class navSidebarSerializer(serializers.ModelSerializer):
created_by = serializers.ReadOnlyField(source='created_by.username')
modified_by = serializers.ReadOnlyField(source='modified_by.username')
class Meta:
model = navSidebars
fields = [
'id',
'name',
'description',
'active',
'visible',
'enabled',
'can_delete',
'fk_navsidebar',
'created_by',
'created_when',
'modified_by',
'modified_when'
]
class navSidebarItemsSerializer(serializers.ModelSerializer):
created_by = serializers.ReadOnlyField(source='created_by.username')
modified_by = serializers.ReadOnlyField(source='modified_by.username')
class Meta:
model = navSidebarItems
fields = [
'id',
'name',
'description',
'position',
'active',
'visible',
'enabled',
'can_delete',
'created_by',
'created_when',
'modified_by',
'modified_when'
]
class navSidebarMapsSerializer(serializers.ModelSerializer):
created_by = serializers.ReadOnlyField(source='created_by.username')
modified_by = serializers.ReadOnlyField(source='modified_by.username')
class Meta:
model = navSidebarMaps
fields = [
'id',
'fk_navsidebar',
'fk_navsidebar_items',
'created_by',
'created_when',
'modified_by',
'modified_when'
]

Related

Get multipe images urls on Django RestFramework

I'm using Django RestFramework to create a simple eCommerce API where one product could have many images and I would like to get the URLs of all these images on a json field.
For now, I got the first image url using "imagembicicleta_set.all.first.image.url" on the serializer, but I need all URLs list:
{
"count": 7,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"nome": "Specialized Roubaix",
"marca__nome": "Specialized",
"categoria": "Bicicletas de Estrada",
"atividades": [
1
],
"terrenos": [
"Asfalto"
],
"preco": "16999.00",
"ano": 1,
"absolute_url": "/bicicletas/Specialized/specialized-roubaix-2020/",
"img_url": "/media/images/bicicletas/roubaix1.jpeg"
},
{
"id": 2,
"nome": "Specialized Roubaix Sport",
"marca__nome": "Specialized",
Following how is my setup:
Models.py
class Bicicleta(models.Model):
id = models.AutoField(primary_key=True)
nome = models.CharField(max_length=200, blank=False, null=False)
slug = models.SlugField(unique=True)
status = models.IntegerField(choices=STATUS_CHOICES, default=1, blank=False, null=False)
descricao = RichTextField(blank=True, null=True)
marca = models.ForeignKey(MarcaBicicleta, blank=True, null=True, on_delete=models.SET_NULL)
...
class ImagemBicicleta (models.Model):
bicicleta = models.ForeignKey(Bicicleta, default=None, on_delete=models.CASCADE)
image = models.ImageField(upload_to='images/bicicletas')
Serializer.py
class BicicletasSerializer(serializers.ModelSerializer):
marca__nome = serializers.CharField(source='marca.nome')
categoria = serializers.CharField(source='categoria.nome')
terrenos = serializers.StringRelatedField(many=True)
absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
img_url = serializers.URLField(source='imagembicicleta_set.all.first.image.url', read_only=True) #I could get the first image using this
class Meta:
model = Bicicleta
fields = ['id', 'nome', 'marca__nome', 'categoria', 'atividades', 'terrenos', 'preco', 'ano', 'absolute_url', 'img_url']
views.py
class BicicletasView(generics.ListAPIView):
serializer_class = BicicletasSerializer
queryset = Bicicleta.objects.all()
filter_backends = (DjangoFilterBackend, SearchFilter)
filterset_fields = ['marca', 'terrenos', 'status']
search_fields = {'nome': ['icontains'], }
How could I get all images URLs in the field?
Per example, if a product has 3 different images, I would expect to have the img field like this:
"img_url": [ "/media/images/bicicletas/roubaix1.jpeg","/media/images/bicicletas/roubaix2.jpeg","/media/images/bicicletas/roubaix3.jpeg" ],
You can add a method serializer which will collect all the urls for each individual object like this:
class BicicletasSerializer(serializers.ModelSerializer):
marca__nome = serializers.CharField(source='marca.nome')
categoria = serializers.CharField(source='categoria.nome')
terrenos = serializers.StringRelatedField(many=True)
absolute_url = serializers.URLField(source='get_absolute_url', read_only=True)
img_url = serializers.SerializerMethodField()
def get_image_url(self , instance):
return ImagemBicicleta.objects.filter(bicicleta=instance).values_list('image',flat=True)
class Meta:
model = Bicicleta
fields = ['id', 'nome', 'marca__nome', 'categoria', 'atividades', 'terrenos', 'preco', 'ano', 'absolute_url', 'img_url']

Unable to populate images in Django Rest Serializer

I'm developing a REST API using Django Rest Framework but I'm unable to populate image in Feed Serializer
Django Version: 3.1.7
Python Version: 3.9.2
Models:
class User(AbstractUser):
age = models.PositiveIntegerField(null=True)
address = models.TextField(null=True)
email = models.EmailField(_('email address'), unique=True, null=False)
first_name = models.CharField(_('first name'), max_length=150, blank=False, null=False)
last_name = models.CharField(_('last name'), max_length=150, blank=False, null=False)
image = models.ImageField(upload_to='storage', null=True)
class Feed(models.Model):
description = models.TextField()
likes_count = models.PositiveIntegerField(default=0, null=True)
comments_count = models.PositiveIntegerField(default=0, null=True)
updated_at = models.DateTimeField(auto_now=True)
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user')
tags = models.ManyToManyField(User, related_name='tags', blank=True)
class FeedImage(models.Model):
path = models.ImageField(upload_to='storage', null=False)
post = models.ForeignKey(Feed, on_delete=models.CASCADE, null=False, default='')
Serializers:
class FeedUserSerializer(ModelSerializer):
class Meta:
model = User
fields = ('id', 'first_name', 'last_name', 'image')
class FeedImageSerializer(serializers.ModelSerializer):
class Meta:
model = FeedImage
fields = ('id', 'path', 'post')
class FeedSerializer(ModelSerializer):
user = FeedUserSerializer()
images = FeedImageSerializer(many=True, read_only=True)
class Meta:
model = Feed
fields = ('id', 'description', 'comments_count', 'likes_count', 'updated_at', 'created_at',
'tags', 'images', 'user')
View:
class FeedsListView(generics.ListAPIView):
queryset = Feed.objects.all()
return FeedSerializer
Problem: I get this result without images
[{
"id": 1,
"description": "Hello world",
"comments_count": 0,
"likes_count": 0,
"updated_at": "2021-04-26T03:01:44.219235Z",
"created_at": "2021-04-26T03:01:44.219235Z",
"tags": [],
"user": {
"id": 1,
"first_name": "ZAIN",
"last_name": "REHMAN",
"image": "http://192.168.88.28:8000/storage/1_Facebook_1.jpg"
}
}]
Expected Output:
[{
"id": 1,
"description": "Hello world",
"comments_count": 0,
"likes_count": 0,
"updated_at": "2021-04-26T03:01:44.219235Z",
"created_at": "2021-04-26T03:01:44.219235Z",
"tags": [],
"user": {
"id": 1,
"first_name": "abc_first",
"last_name": "cdef_last",
"image": "http://192.168.88.28:8000/storage/1_Facebook_1.jpg"
},
"images": [
{
"id": 1,
"post": "1",
"path": "imagelocation"
},
{
"id": 2,
"post": "1",
"path": "imagelocation 2"
}
]
}]
Any solution or any workaround will be appreciated.
Ignore this: Bulk text to resolve StackOverflow problem
industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
You can use SerializerMethodField for this.
class FeedSerializer(ModelSerializer):
images = serializers.SerializerMethodField()
class Meta:
model = Feed
fields = ('id', 'description', 'comments_count', 'likes_count', 'updated_at', 'created_at',
'tags', 'images', 'user')
def get_images(self, obj):
return obj.feedimage_set.all().values('id', 'path', 'post')

Django Serializer's Source argument is not working

I am trying to get all the bills and their customer-related details (i.e. 'customer_code', 'email' etc.) with it.
However, source='user.customer_code does not seem to have any effect at all. What am I missing?
I have been following along this:
this stackoverflow post with no luck.
My two models:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(db_index=True, unique=True, max_length=200)
customer_code = models.CharField(max_length=300, blank=True, null=True, default=None)
class Bill(models.Model):
customer = models.ForeignKey(
User, on_delete=models.CASCADE, blank=True, null=True, related_name="customer_bill"
)
payable_amount = models.DecimalField(max_digits=10, decimal_places=2, default=0)
View:
class BillView(APIView):
def get(self, request, format=None):
q = Bill.objects.all().select_related('customer')
s = BillSerializer(q, many=True)
return JsonResponse({
"bill": s.data
})
Serializer:
class BillSerializer(serializers.ModelSerializer):
customer_code = serializers.CharField(source='user.customer_code', read_only=True)
class Meta:
model = Bill
fields = ('id','payable_amount','customer_code') # binding customer_code here
Current Output:
"bill": [
{
"id": 1,
"payable_amount": "1000.00"
},
{
"id": 2,
"payable_amount": "2000.00"
}
]
Expected Result:
"bill": [
{
"id": 1,
"payable_amount": "1000.00",
"customer_code": "CUS10001" # want this to be attached
},
{
"id": 2,
"payable_amount": "2000.00",
"customer_code": "CUS10002" # want this to be attached
}
]

How to join multiple Model in Django Rest Framework serializer

In Django, I have the following models.
class Property(models.Model):
address1 = models.CharField(max_length=512)
address2 = models.CharField(max_length=128, blank=True, null=True)
property_type = models.ForeignKey('PropertyType', models.DO_NOTHING, null=True, blank=True, default=None)
geo_info = models.ForeignKey(GeoInfo, models.DO_NOTHING)
class Meta:
indexes = [
models.Index(fields=['address1', ]),
]
db_table = 'property'
class PropertyType(models.Model):
name = models.CharField(max_length=128, blank=True, null=True)
category = models.CharField(max_length=45, blank=True, null=True)
color = models.CharField(max_length=45, blank=True, null=True)
class Meta:
db_table = 'property_type'
indexes = [
models.Index(fields=['name', ]),
]
class GeoInfo(models.Model):
zipcode = models.CharField(max_length=25, blank=True, null=True)
city = models.CharField(max_length=45, blank=True, null=True)
state = models.CharField(max_length=45, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, null=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
class Meta:
db_table = 'geo_info'
indexes = [
models.Index(fields=['zipcode', ]),
models.Index(fields=['city', ]),
]
def __str__(self):
return '%s %s' % (self.zipcode, self.city)
I'm trying to use Django REST Framework to serialize PROPERTY model as follows.
class GeoSerializer(serializers.ModelSerializer):
class Meta:
model = GeoInfo
fields = ['zipcode', 'city', 'state']
class PropertyTypeSerializer(serializers.ModelSerializer):
class Meta:
model = PropertyType
class PropertySerializer(serializers.ModelSerializer):
address_geo = GeoSerializer(many=False, read_only=True)
prop_type = PropertyTypeSerializer(many=True, read_only=True)
class Meta:
model = Property
fields = ['id', 'address1', 'address_geo', 'prop_type']
My expected outcome for each item is like
{
"id": 1,
"address1": "test address",
"geo_info":{
"zipcode":"12121",
"city":"12121",
"state":"12121",
},
"property_type": "unknown",
}
and currently, I'm just getting the property data like
{
"id": 1,
"address1": "test address"
}
So can you help me figure out how to serialize these models?
Also, it would be great if you can help me how to flatten the outcome and avoid nested node if possible
your question is not clear fully. You asked for no nesting. This may help you -
class PropertySerializer(serializers.ModelSerializer):
zipcode=serializers.SerializerMethodField()
city=serializers.SerializerMethodField()
state=serializers.SerializerMethodField()
class Meta:
model = Property
fields = ('id','address1','zipcode','city','state','property_type')
def get_zipcode(self,instance):
return instance.geo_info.zipcode
def get_city(self,instance):
return instance.geo_info.city
def get_state(self,instance):
return instance.geo_info.state

Python Django rest-framework serializer omitting field

I'm facing a problem using python2.7 with django rest-framework. When I serialize my JSON data, a field is omitted by the serializer and I don't understand why. Here is some details.
The missing field is "country". When I'm doing POST or PUT requests on /campaigns/:id
class CampaignSerializer(serializers.HyperlinkedModelSerializer):
created_by = UserFullSerializer(read_only=True)
country = CountrySerializer(read_only=True)
class Meta:
model = Campaign
fields = ('id', 'created_by', 'name', 'media', 'status', 'begin', 'end', 'country')
class CampaignFullSerializer(serializers.HyperlinkedModelSerializer):
client = ClientSerializer(read_only=True)
collection = CollectionSerializer(read_only=True)
created_by = UserFullSerializer(read_only=True)
updated_by = UserFullSerializer(read_only=True)
channels = ChannelSerializer(read_only=True, many=True)
country = CountrySerializer(read_only=True)
class Meta:
model = Campaign
fields = ('id',
'client',
'name',
'media',
'status',
'begin',
'end',
'created_at',
'created_by',
'updated_at',
'updated_by',
'collection',
'channels',
'country')
class CountrySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Country
fields = ('id', 'name', 'code')
class Country(models.Model):
name = models.CharField(max_length=255)
code = models.CharField(max_length=255)
class Campaign(models.Model):
name = models.CharField(max_length=255)
media = models.IntegerField(choices=constant.MEDIA_CHOICES, default=0)
status = models.IntegerField(choices=constant.STATUS_CHOICES, default=2)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.ForeignKey(User, blank=True, null=True, related_name="created_by")
updated_at = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated_by = models.ForeignKey(User, blank=True, null=True, related_name="updated_by")
client = models.ForeignKey(client.Client)
begin = models.DateField(blank=True, null=True)
end = models.DateField(blank=True, null=True)
collection = models.ForeignKey(collection.Collection, blank=True, null=True)
country = models.ForeignKey(country.Country, blank=True, null=True)
mediaplan = models.ForeignKey(mediaplan.Mediaplan, blank=True, null=True, default=None)
channels = models.ManyToManyField(channel.Channel)
When I'm doing POST on /campaign/id with the following JSON, everything works except the country field.
{
...
"channels": [],
"country": {
"id": 74,
"name": "France",
"code": "FR"
}
On the controller side when I print the request.data I got all the fields. I'm not overriding the create method of the controller.
{
...
u'country': {u'code': u'AL', u'id': 3, u'name': u'Albania'}
}
My controller looks like:
class CampaignViewSet(viewsets.ModelViewSet):
queryset = Campaign.objects.all()
serializer_class = CampaignSerializer
def create(self, request):
logger.info(request.data)
return super(CampaignViewSet, self).create(request, *args, **kwargs)
I tried to override the create method of my CountrySerializer and when I print the content of validated_data, the country field is missing in the OrderedDict..
class CountrySerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Country
fields = ('id', 'name', 'code')
def create(self, validated_data):
logger.info(validated_data)
I'm really lost, I can't find my mistake, maybe you will. Thanks for your time.
Your CountrySerializer is read only as a nested serializer by default (per http://www.django-rest-framework.org/api-guide/relations/#nested-relationships) so you have to override the create/update method of the Campaign serializer for POST/PUT. You've tried to override it on the Country serializer instead.

Categories