在Django REST

时间:2015-12-08 17:02:38

标签: django nested serializer

以下问题让我真正疯狂......

我有三种模式:

  • 订单
  • 目标
  • ConstructionSite

订单有“日期”和“用户”以及许多“目标”。目标本身包含一些信息和“ConstructionSite”的关键。

我正在尝试使用Angular构建RESTful API。到现在为止还挺好。我也知道必须手动构建可写嵌套序列化器。

我的模特:

class ConstructionSite(models.Model):
    cost_unit_nr = models.CharField(primary_key=True)

class Order(models.Model):
    date = models.DateField("Datum")
    users = models.ManyToManyField(User)

class Target(models.Model):
    cost_unit_nr = models.OneToOneField(ConstructionSite)
    order = models.ForeignKey(Order, related_name="targets")

我的序列化工具*

class ConstructionSiteIDSerializer(serializers.HyperlinkedModelSerializer):
    cost_unit_nr = serializers.CharField()

    class Meta:
        model = ConstructionSite
        fields = ('cost_unit_nr',)

class TargetSerializer(serializers.ModelSerializer):
    cost_unit_nr = ConstructionSiteIDSerializer()
    id = serializers.IntegerField()

    class Meta:
        model = Target
        fields = ('id', 'cost_unit_nr', 'services', 'sequence_number', 'published')


class OrderSerializer(serializers.ModelSerializer):
    users   = serializers.PrimaryKeyRelatedField(queryset=User.objects.all(), many=True)
    targets = TargetSerializer(many=True, required=True, read_only=False)

    class Meta:
        model = Order
        fields = ('id', 'date', 'targets', 'users')

    def update(self, instance, validated_data):
        targets_data = validated_data.pop('targets')
        users_data = validated_data.pop('users')

        instance.date = validated_data.get('date', instance.date)

        # User handling ... (not shown here)

        # Targets
        instance.targets = []

        for target in targets_data:
            cs_data = target.get('cost_unit_nr')

            new_target, created = Target.objects.update_or_create(id=target.get('id', False))

            cs_instance, created = ConstructionSite.objects.get_or_create(cost_unit_nr=cs_data.get('cost_unit_nr'))

            new_target.cost_unit_nr = cs_instance
            new_target.services = target.get('services')
            new_target.published = target.get('published', False)
            new_target.sequence_number = target.get('sequence_number', 0)

            instance.targets.add(new_target)


        instance.save()

        return instance

数据

我正在尝试更新现有订单。它已经有一个Target,我想再添一个:

[{"id":13,
  "date":"2015-12-03",
  "targets":[
      {
      "id":15,
      "cost_unit_nr": "cost_unit_nr":"3"},
      "services":"whatever",
      "sequence_number":0, 
      "published":false},
      {
      "cost_unit_nr": "cost_unit_nr":"4"},
      "services":"still whatever",
      "sequence_number":0, 
      "published":false}],
  "users":[1]}]

cost_unit_nr是ConstructionSite对象的id。

目标的第二个条目当然没有ID,当我调用update_or_create时应该创建它...但是抛出了IntegrityError:

('weekplans_target.cost_unit_nr_id may not be NULL',)

“weekplans”是我的应用程序的名称。

第一:为什么? 第二:为什么要target.cost_unit_nr_id?我在任何地方都没有这个领域。为什么_id被添加到cost_unit_nr

谢谢你们!

0 个答案:

没有答案