0

I am having two model definitions CTA and VariationCTA as follows:

class CTA(AbstractTimestampClass):
    page = models.ForeignKey(Page,                               
    related_name='related_page_ctas')
    css_path = models.CharField(max_length=1000)
    source_content = models.TextField()
    category = models.CharField(max_length=100)
    subcategory = models.CharField(max_length=100)
    ctype = models.CharField(max_length=25, choices=CTA_TYPE_CHOICES, default=CTA_TYPE_HTML)
    deleted = models.BooleanField(default=False)


class VariationCTA(AbstractTimestampClass):
    variation = models.ForeignKey(Variation,   
    related_name='related_variation_ctas')
    cta = models.ForeignKey(CTA,    
    related_name='related_cta_variation_ctas')
    target_content = models.TextField()
    deleted = models.BooleanField(default=False)

To combine these two models I am making use of a serializer as follows:

class PageCTASerializerForVariation(serializers.ModelSerializer):
    variation = serializers.SlugRelatedField(
    read_only=True,
    many=True,
    source='related_cta_variation_ctas',
    slug_field='variation'
 )

    target_content = serializers.SlugRelatedField(
    read_only=True,
    many=True,
    source='related_cta_variation_ctas',
    slug_field='target_content'
 )

    class Meta:
        model = CTA
        fields = ('id','css_path', 'source_content', 'category','subcategory', 'ctype', 'target_content','variation')

The serializer is invoked as follows:

PageCTASerializerForVariation(page.related_page_ctas.get(CTA Model Instance).data

When the API is used from the browser the serialized instance is a follows:

{'category': "Title", 'subcategory': "Page Title",   
'target_content': u'asjbsnak', 'source_content': u'hsdvhas', 
'ctype': 'HTML', 'css_path': u'asdjbjsdansdas>sakdbja>dafhds', 
'id': 1}

But when I call it through the test case,the serialized instance is as follows:

{'category': u"[u'Title']", 'subcategory': u"[u'Page Title']",     
'target_content': u'asjbsnak', 'source_content': u'hsdvhas', 
'ctype': 'HTML', 'css_path': u'asdjbjsdansdas>sakdbja>dafhds', 
'id': 1}

Notice the category and subcategory fields in the serialized instances above.

In the normal API call they are output as a normal string(this is the expected output),whereas in the test case call they are output as a list with extra characters(unexpected).

This is causing my test case assertions to fail.

Please Help!!

Vaibhav Shah
  • 131
  • 1
  • 6

0 Answers0