将Django JSONField作为YAML处理

时间:2018-04-08 16:23:04

标签: json django forms yaml models

在Django中,Postgres JSONField模型字段在CharField表单字段中显示为普通JSON文本。

我想将CharField中的数据显示为YAML文本(同时在内部保持JSON格式),同时保存将其转换回JSON,如:

yaml.dump(json.loads(value))

当前:

Expected

必要:Was

如何做到这一点?

感谢。

1 个答案:

答案 0 :(得分:0)

解决方案是继承JSONField(包括模型和表单),并将json替换为yaml。作为单独的包装: https://github.com/mike-tk/django-yamlfield

# Function  to Determine each student's average test score using NESTED WHILE LOOPS
def calculateStudentsAverageTestScore():
    global testNum, numStudents, student;  # make variables available for other functions/subtasks


# Get a student's test scores
print();
print("Student number", student);
print("-" * 50);

testNum = 1; #Initialize testNum lcv for the inner loop used to control the number of tests per student
while (testNum <= numTestScores): # test testNum

            #Check to make sure data is numeric
                while True:
                    try:
                        print("Test number", testNum, end = " ");
                        score = float(input(": "));
                    except ValueError:
                        print("Error: Invalid data - enter a numeric value!");
                        continue;
                    else:
                        break;
                    #end while loop

              # Calculate the average test score for this student.
                average = total / numTestScores; 
                testNum = testNum + 1; # counts the number of tests per student (updates testNum lcv)
              # End caluclateStudentsAverageTestScore