想为我的Django API创建一个POST方法

时间:2017-09-14 03:53:57

标签: json django post django-rest-framework add

我想在设置API时为我的Django项目创建一个POST方法。我已经尝试为create创建覆盖方法,但它给了我一个完整性约束。我想发一篇文章,将所有字段存储在一个整洁的JSON中。我不确定是不是因为导致问题的许多地方。谢谢你的进步。

Models.py
class Results(models.Model):
    type = models.CharField(max_length=200, blank=True)
    question = models.CharField(max_length=200, blank=True)
    title = models.CharField(max_length=200, blank=True)
    ytid = models.CharField(max_length=200, blank=True)
    correct_answer = models.CharField(max_length=200, blank=True)
    incorrect_answers = ArrayField(models.CharField(max_length=200), 
    blank=True)

    def __str__(self):
        return '{}, {}, {}, {}, {}, {}'.format(self.type, self.question, 
self.title, self.ytid, self.correct_answer, self.incorrect_answers)

class Simulation(models.Model):
    sequence = models.PositiveIntegerField(unique=False)
    method = models.CharField(max_length=200)
    results = models.ForeignKey(Results, on_delete=models.CASCADE)

    class Meta:
        ordering = ['sequence']

    def __str__(self):
        return '{}'.format(self.sequence)

class respond_code(models.Model):
    respond_code = models.PositiveIntegerField(primary_key=True)
    simulation = models.ManyToManyField(Simulation)

    class Meta:
        ordering = ['respond_code']

    def __str__(self):
        return '{}'.format(self.respond_code)

Serializers.py
class ResultsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Results
        fields = 
('type','question','title','ytid','correct_answer','incorrect_answers')

class SimulationSerializer(serializers.ModelSerializer):
    results = ResultsSerializer(many=False)
    class Meta:
        model = Simulation
        fields = ('sequence','method','results')


class respond_codeSerializer(serializers.ModelSerializer):
    simulation = SimulationSerializer(many=True)
    class Meta:
        model = respond_code
        fields = ('respond_code', 'simulation')

    def create(self, validated_data):
        simulations_data = validated_data.pop('simulation')
        simulation = Simulation.objects.create(**validated_data)
        for i in simulations_data:
            Simulation.objects.create(simulation=simulation, **i)
            for data in simulations_data:
                Simulation.sequence.add(simulation,**data)
                Simulation.method.add(simulation,**data)
                simulation.save()

        #return Simulation.objects.create(simulation=simulation, **i)
        return simulation

我已对serializer.py进行了修改

class respond_codeSerializer(serializers.ModelSerializer):
    simulation = SimulationSerializer(many=True)
    class Meta:
        model = respond_code
        fields = ('respond_code', 'simulation')

    def create(self, validated_data):
        simulations_data = validated_data.pop('simulation')
        simulation = respond_code.objects.create(**validated_data)
        simulation.save()
        for Simulation in simulations_data:
            #d = dict(i)
            b = 
Simulation.objects.get_or_create(sequence=Simulation['sequence'])
            Simulation.add(b)
            #respond_code.save()
        return simulation

1 个答案:

答案 0 :(得分:0)

尝试这样的事情,

class respond_codeSerializer(serializers.ModelSerializer):

    simulation = SimulationSerializer(many=True)

    class Meta:
        model = respond_code
        fields = ('respond_code', 'simulation')

    def create(self, validated_data):
        simulations_data = validated_data.pop('simulation')
        instance = super(respons_codeSerializer, self).create(validated_data)
        for simulation in simulations_data:
            result_data = simulation.pop('results')
            result, c = Results.objects.get_or_create(**result_data)
            sim, c = Simulation.objects.get_or_create(results=result, **simulation)
            instance.simulation.add(sim)
        instance.save()
        return instance