具有额外字段的DRF多态多对多

时间:2019-01-11 08:14:05

标签: python django django-rest-framework django-polymorphic

我正在研究Django Rest Framework,以构建可以处理电子组件物料清单(BoM)的api。

我正在使用django-polymorphic和django-rest-polymorphic,因此我可以将多态模型用于组件(它们具有共享的属性,但是我仍然希望以相同的方式处理它们。多态模型非常适合这个目的)。

在我引入具有“通过”属性的多对多关系之前,一切都很好。我想要的是一个BoM,其中包含几个不同的组件,每个组件都有一定数量,例如BoM1具有2x470k电阻器和3x 100uF电容器。

models.py :(为了使这篇文章不再是史诗小说,做了一些删节)

class BoM(models.Model):
    """Bill of Materials: a list of all parts and their quantities for a given pedal"""
    pedal = models.ForeignKey(Pedal, on_delete=models.CASCADE)
    variant = models.CharField(max_length=100, blank=True)
    electronic_components = models.ManyToManyField(
        'ElectronicComponent', through='ElectronicComponentQty', blank=True)

    class Meta:
        verbose_name = 'Bill of materials'
        verbose_name_plural = 'Bills of materials'

    def __str__(self):
        return str(self.pedal)


class ElectronicComponent(PolymorphicModel):
    """Shared data model for electronic components"""
    value = models.CharField(max_length=20)
    datasheet = models.FileField(upload_to='uploads/components', blank=True)

    def __str__(self):
        return self.value


class ElectronicComponentQty(models.Model):
    """Combination of resistor and quantity"""
    bom = models.ForeignKey(BoM, on_delete=models.CASCADE)
    component = models.ForeignKey(
        ElectronicComponent, on_delete=models.CASCADE)
    quantity = models.PositiveIntegerField(default=1)

    class Meta:
        verbose_name = 'Elecronic component quantity'
        verbose_name_plural = 'Electronic component quantities'


class Resistor(ElectronicComponent):
    """Resistor data model"""
    WATTAGES = [('1/4w', '1/4w'), ('1/8w', '1/8w')]

    wattage = models.CharField(max_length=4, choices=WATTAGES, default='1/4w')



class Capacitor(ElectronicComponent):
    """Capacitors (all kinds)"""
    VOLTAGE_RATINGS = [
        ('16V', '16V'),
        ('35V/50V', '35V/50V'),
    ]

    CAP_TYPES = [
        ('ceramic disk', 'ceramic disk'),
        ('film', 'film'),
        ('electrolytic', 'electrolytic'),
        ('tantalum', 'tantalum'),
        ('other', 'other'),
    ]

    capacitor_type = models.CharField(
        max_length=20, choices=CAP_TYPES, default='film')
    voltage_rating = models.CharField(
        max_length=10, choices=VOLTAGE_RATINGS, blank=True)

serializers.py:

class ElectronicComponentSerializer(serializers.ModelSerializer):
    class Meta:
        model = ElectronicComponent
        fields = '__all__'


class ElectronicComponentQtySerializer(serializers.ModelSerializer):
    class Meta:
        model = ElectronicComponentQty
        fields = '__all__'


class BoMSerializer(serializers.ModelSerializer):
    electronic_components = ElectronicComponentQtySerializer(
        many=True, read_only=True)

    class Meta:
        model = BoM
        fields = '__all__'

class ResistorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Resistor
        fields = '__all__'


class CapacitorSerializer(serializers.ModelSerializer):
    class Meta:
        model = Capacitor
        fields = '__all__'


class ElectronicComponentPolySerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        Resistor: ResistorSerializer,
        Capacitor: CapacitorSerializer,
    }

使用此代码,我可以毫无问题地创建EletronicComponentQty对象。但是,当我尝试(通过序列化程序)列出BoM时,得到:     / pedalparts / boms /中的AttributeError     尝试获取字段bom的值时遇到AttributeError     序列化器ElectronicComponentQtySerializer。     序列化程序字段的名称可能不正确,不匹配任何     Capacitor实例上的属性或键。     原始异常文本为:“电容器”对象没有属性     “宝”。

有人知道我该如何解决?我愿意接受任何可以使这项工作生效的更改。

1 个答案:

答案 0 :(得分:0)

由于electronic_components模型上的BoM指的是ElectronicComponent模型,因此它不应该使用ElectronicComponentQtySerializer而是可以序列化正确实例(最可能是{{ 1}}或ElectronicComponentSerializer