我正在 -
[
{
"id": 2,
"url": "https://emiactech.com/",
"title": "",
"blog_post": 0,
"index_count": 0,
"external_urls": "[{'url': '#', 'page': 'https://emiactech.com/', 'title': u''}, {'url': 'https://www.facebook.com/EMIACTechnologies/', 'page': 'https://emiactech.com/', 'title': u'Facebook'}, {'url': 'https://twitter.com/emiactech', 'page': 'https://emiactech.com/', 'title': u'Twitter'}, {'url': 'mailto:sales@emiactech.com', 'page': 'https://emiactech.com/', 'title': u'sales@emiactech.com'}, {'url': 'http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226?ref=ThemeFusion', 'page': 'https://emiactech.com/about-us/', 'title': u''}, {'url': '#tab-bdf4143f2c7b5609720', 'page': 'https://emiactech.com/services/', 'title': u'Web Design and Development'}, {'url': '#tab-70d2572b15f9a5ac477', 'page': 'https://emiactech.com/services/', 'title': u'Content Development'}, {'url': '#tab-c59a00bf26295e6ed39', 'page': 'https://emiactech.com/services/', 'title': u'Digital Marketing'}, {'url': 'https://emiactech.com', 'page': 'https://emiactech.com/cqpim-client/', 'title': u'Home'}, {'url': 'http://themetf.com', 'page': 'https://emiactech.com/portfolio-items/humsafar-love/', 'title': u' theme-tf'}, {'url': 'http://shades.salon', 'page': 'https://emiactech.com/portfolio_category/jquery/', 'title': u'http://shades.salon'}, {'url': 'http://Project%20URL', 'page': 'https://emiactech.com/portfolio_category/jquery/', 'title': u' View Project '}, {'url': 'http://samacharjagat.com', 'page': 'https://emiactech.com/portfolio_category/jquery/', 'title': u'http://samacharjagat.com'}]",
"external_count": 13
}]
serializer.data
中的。 external_urls密钥数据是字符串的类型。我需要以列表或字典的形式提供这样的关键数据;
[{'url': '#', 'page': 'https://emiactech.com/', 'title': u''}, {'url': 'https://www.facebook.com/EMIACTechnologies/', 'page': 'https://emiactech.com/', 'title': u'Facebook'}, {'url': 'https://twitter.com/emiactech', 'page': 'https://emiactech.com/', 'title': u'Twitter'}, {'url': 'mailto:sales@emiactech.com', 'page': 'https://emiactech.com/', 'title': u'sales@emiactech.com'}, {'url': 'http://themeforest.net/item/avada-responsive-multipurpose-theme/2833226?ref=ThemeFusion', 'page': 'https://emiactech.com/about-us/', 'title': u''}, {'url': '#tab-bdf4143f2c7b5609720', 'page': 'https://emiactech.com/services/', 'title': u'Web Design and Development'}, {'url': '#tab-70d2572b15f9a5ac477', 'page': 'https://emiactech.com/services/', 'title': u'Content Development'}, {'url': '#tab-c59a00bf26295e6ed39', 'page': 'https://emiactech.com/services/', 'title': u'Digital Marketing'}, {'url': 'https://emiactech.com', 'page': 'https://emiactech.com/cqpim-client/', 'title': u'Home'}, {'url': 'http://themetf.com', 'page': 'https://emiactech.com/portfolio-items/humsafar-love/', 'title': u' theme-tf'}, {'url': 'http://shades.salon', 'page': 'https://emiactech.com/portfolio_category/jquery/', 'title': u'http://shades.salon'}, {'url': 'http://Project%20URL', 'page': 'https://emiactech.com/portfolio_category/jquery/', 'title': u' View Project '}, {'url': 'http://samacharjagat.com', 'page': 'https://emiactech.com/portfolio_category/jquery/', 'title': u'http://samacharjagat.com'}]
< --------------------- UPDATE ------------------->
serializer.py
class WebsiteSerializer(serializers.ModelSerializer):
class Meta:
model = Website
fields = '__all__'
models.py
class Website(models.Model):
url = models.CharField(max_length=200)
title = models.CharField(max_length=255, blank=True,)
blog_post = models.IntegerField(default=170, blank=True,)
index_count = models.IntegerField(blank=True,)
external_urls = models.TextField(blank=True, null=True)
external_count = models.IntegerField(blank=True,)
class Meta:
db_table = "website"
答案 0 :(得分:1)
from ast import literal_eval
class WebsiteSerializer(serializers.ModelSerializer):
external_urls = serializers.SerializerMethodField()
def get_external_urls(self, obj):
# return eval(obj.external_urls) will work
return literal_eval(obj.external_urls)