我收到以下错误,但无法找到解决方法
异常类型:TypeError异常值:'EstablishmentType'对象 是不可迭代的
类序列化程序:
class TransactionSerializer(serializers.HyperlinkedModelSerializer):
establishment_from_id = serializers.HyperlinkedRelatedField(view_name='establishment_type', read_only=True, many=True)
class Meta:
model = Transaction
fields = ('id', 'created_date', 'updated_date','establishment_from_id', 'quantity', 'owner_id', 'quantity_missing', 'gps_longitude', 'gps_latitude', 'gps_accuracy', 'type')
模型
class Transaction(models.Model):
created_date = models.DateTimeField()
updated_date = models.DateTimeField()
establishment_from_id = models.ForeignKey(EstablishmentType, related_name="establishment_from")
establishment_to_id = models.ForeignKey(EstablishmentType, related_name="establishment_to")
quantity = models.FloatField()
owner_id = models.ForeignKey(User)
quantity_missing = models.FloatField()
status = models.ForeignKey(TransactionStatus)
gps_longitude = models.CharField(max_length=400)
gps_latitude = models.CharField(max_length=400)
gps_accuracy = models.CharField(max_length=400)
type = models.FloatField()
def __str__(self):
return str(self.id)
class EstablishmentType(models.Model):
name = models.CharField(max_length=200)
#created_by = CreatingUserField(related_name="created_categories")
def __str__(self):
return self.name
谢谢!
答案 0 :(得分:0)
尝试从Serializer中取出many=True
。 establish_from_id是FK而不是M2M,因此对于每个实例,它只有一个值。如果这是EstablishmentType
的序列化程序,则有many=True