Создание пользовательских выходных данных JSON из объектов модели Django - PullRequest
0 голосов
/ 16 марта 2019

Я пытаюсь вывести данные 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)

I 'работает на Django 1.11 и Python 2.7

1 Ответ

0 голосов
/ 16 марта 2019

Я предлагаю вам использовать django rest framework, а затем использовать Modelserializer. Здесь вы можете найти его: https://www.django -rest-framework.org / api-guide / serializers / В вашем случае это будет:

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