从Django模型对象创建自定义JSON输出

时间:2019-03-16 11:44:49

标签: json django serialization django-models django-rest-framework

我正在尝试从我的Question模型输出json数据。我似乎无法正确解决问题。

{"response_code":0,"results":[{"category":"Vehicles","type":"multiple","difficulty":"easy","question":"The Italian automaker Lamborghini uses what animal as its logo?","correct_answer":"Bull","incorrect_answers":["Bat","Horse","Snake"]},{"category":"Mythology","type":"multiple","difficulty":"medium","question":"Who was the Roman god of fire?","correct_answer":"Vulcan","incorrect_answers":["Apollo","Jupiter","Mercury"]},{"category":"General Knowledge","type":"multiple","difficulty":"medium","question":"After how many years would you celebrate your crystal anniversary?","correct_answer":"15","incorrect_answers":["20","10","25"]},{"category":"Entertainment: Video Games","type":"multiple","difficulty":"hard","question":"Which of these online games was originally named LindenWorld in it's early development?","correct_answer":"SecondLife","incorrect_answers":["ActiveWorlds","IMVU","HabboHotel"]},{"category":"History","type":"multiple","difficulty":"hard","question":"When was the SS or Schutzstaffel established?","correct_answer":"April 4th, 1925","incorrect_answers":["September 1st, 1941","March 8th, 1935","February 21st, 1926"]},{"category":"Entertainment: Video Games","type":"multiple","difficulty":"medium","question":"In the video game "League of Legends" which character is known as "The Sinister Blade"?","correct_answer":"Katarina","incorrect_answers":["Shaco","Akali","Zed"]},{"category":"Science & Nature","type":"multiple","difficulty":"easy","question":"What is the standard atomic weight of a Plutonium nucleus?","correct_answer":"244","incorrect_answers":["94","481","128"]},{"category":"Entertainment: Comics","type":"multiple","difficulty":"hard","question":"What is the real hair colour of the mainstream comic book version (Earth-616) of Daredevil?","correct_answer":"Blonde","incorrect_answers":["Auburn","Brown","Black"]},{"category":"Science & Nature","type":"multiple","difficulty":"hard","question":"What does the term "isolation" refer to in microbiology?","correct_answer":"The separation of a strain from a natural, mixed population of living microbes","incorrect_answers":["A lack of nutrition in microenviroments","The nitrogen level in soil","Testing effects of certain microorganisms in an isolated enviroments, such as caves"]},{"category":"Geography","type":"multiple","difficulty":"easy","question":"What is the capital of South Korea?","correct_answer":"Seoul","incorrect_answers":["Pyongyang","Taegu","Kitakyushu"]}]}

类别,类型和难度暂时可以忽略。

Models.py

class Question(models.Model):
    question = models.CharField("Question",max_length=255,blank=True, null=True)
    correct_answer = models.CharField("Correct Answer",max_length=255,blank=True, null=True)
class Incorrect(models.Model):
    incorrect_answers = models.CharField("Incorrect Answer",max_length=255,blank=True, null=True)
    question = models.ForeignKey(Question, on_delete=models.CASCADE,blank=True, null=True)

我正在django 1.11和python 2.7上运行

1 个答案:

答案 0 :(得分:0)

我建议您使用django rest框架,然后使用Modelserializer。在这里您可以找到它:https://www.django-rest-framework.org/api-guide/serializers/ 在您的情况下,将是:

class QuestionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = ('questions', 'correct_answer')