【问题标题】:Create Custom JSON output from Django Model objects从 Django 模型对象创建自定义 JSON 输出
【发布时间】:2019-08-07 08:17:54
【问题描述】:

我正在尝试从我的问题模型中输出 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"]}]}

现在可以忽略类别、类型和难度。

模型.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 上运行

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 2015-09-20
      • 1970-01-01
      • 2015-04-16
      • 1970-01-01
      相关资源
      最近更新 更多