0

Need some advice. I set up seralization. There are no errors. But at the output I get empty tags. I broke my head, what am I doing wrong?

models.py:

class kv(models.Model):
    title = models.CharField(max_length=200)
    price = models.IntegerField()
    address = models.CharField(max_length=200)
    property_type = models.CharField(choices=realty_type_choices_admin, default='kv',
                                     max_length=200, blank=True)

    country = models.CharField(default='Россия', max_length=200)
    region = models.CharField(max_length=200)
    state = models.CharField(choices=state_choices_admin, default='DGO', max_length=200, blank=True, null=True)
    locality_name = models.CharField(max_length=200, blank=True, null=True)
    address_xml = models.CharField(max_length=200, blank=True, null=True)
    city = models.CharField(max_length=100, blank=True, null=True)

serializers.py

from rest_framework import serializers
from listings.models import kv

class kvSerializerLocation(serializers.ModelSerializer):
    class Meta:
        model = kv
        fields = ['country', 'region', 'state', 'locality_name', 'address_xml', 'city']


class kvSerializer(serializers.ModelSerializer):
    category = serializers.CharField(source='get_property_type_display')
    url = serializers.CharField(source='get_absolute_url', read_only=True)
    country = kvSerializerLocation()

    class Meta:
        model = kv
        fields = ['title', 'price', 'address', 'category', 'url', 'country']

views.py

from listings.models import *
from rest_framework import viewsets
from rest_framework_xml.renderers import XMLRenderer
from .serializers import kvSerializer

class KvXMLRenderer(XMLRenderer):
    root_tag_name = 'feed'
    item_tag_name = 'offer'

    def _to_xml(self, xml, data):
        super()._to_xml(xml, data)

class kvViewSet(viewsets.ModelViewSet):
    queryset = Kvartiry.objects.all().filter(is_published=True)
    serializer_class = kvSerializer
    renderer_classes = [KvXMLRenderer]

Result:

<country>
    <state/>
    <locality_name/>
    <address_xml/>
    <city/>
</country>

It’s strange. Tags are empty, there is no region tag at all

Thank!

3
  • In views.py class kvViewSet make the attributes inside Class Meta, i think thats the issue. Commented Aug 29, 2019 at 7:01
  • Moha369, hello and sorry! Honestly, I do not quite understand what you mean. Commented Aug 29, 2019 at 7:58
  • Write Class Meta: inside kvViewSet it type the attributes you mentioned in kvViewSet like queryset and the 2 others. Commented Aug 29, 2019 at 8:13

1 Answer 1

1

I don't think your implemenration would work for country because its a field, and there is no way to map that country value to your kv instance which can be utilized by kvSerializerLocation. Instead use SerializerMethodField:

class kvSerializer(serializers.ModelSerializer):  # please use PascalCase for defining class name
    category = serializers.CharField(source='get_property_type_display')
    url = serializers.CharField(source='get_absolute_url', read_only=True)
    kv_country = serializers.SerializerMethodField()

    class Meta:
        model = kv
        fields = ['title', 'price', 'address', 'category', 'url', 'kv_country']

    def get_kv_country(self, obj):
        return kvSerializerLocation(obj).data
Sign up to request clarification or add additional context in comments.

2 Comments

Ruddra, hello! This is awesome! You are my hero! It is working! Not fully, but it works! Now 3 out of 5 tags are displayed. Does not show their tag data <locality_name/>, <address_xml/>
not sure what might be wrong here. Please check if there are Data for these fields in DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.