0

我正在修补 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)

序列化程序.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(通过序列化程序)时,我得到: AttributeError at /pedalparts/boms/ Got AttributeError when trying to get a value for field bomon serializer ElectronicComponentQtySerializer。序列化器字段可能命名不正确,并且与Capacitor实例上的任何属性或键都不匹配。原始异常文本是:“电容器”对象没有属性“bom”。

任何人都知道我该如何解决这个问题?我愿意接受任何使这项工作发生的变化。

4

1 回答 1

0

正如模型electronic_componentsBoM所指的ElectronicComponent模型,它不应该使用ElectronicComponentQtySerializer可以序列化正确实例的模型,很可能是ElectronicComponentSerializerElectronicComponentPolySerializer.

于 2019-01-11T11:17:30.703 回答