Django Nested Serializer找不到属性

时间:2017-07-20 09:42:16

标签: django serialization django-rest-framework

这是我的模特

class OWNER_STORE(models.Model):
    _id = models.CharField(max_length=120, unique=True, default=uuid.uuid4)
    MSE_STORE_TYPE = models.ForeignKey(STORE_TYPE)
    MSE_OWNER_INFO = models.ForeignKey(OWNER_INFO, related_name='STORES')
    STORE_NAME = models.CharField("STORE_NAME", max_length=250, null=True, blank=True)
    STORE_ID = models.IntegerField("STORE_ID", null=True, blank=True)
    STORE_ADDRESS_HOUSE = models.CharField("STORE_ADDRESS_HOUSE", max_length=250, null=True, blank=True)
    STORE_ADDRESS_ROAD = models.CharField("STORE_ADDRESS_ROAD", max_length=250, null=True, blank=True)
    STORE_ADDRESS_VILLAGE = models.CharField("STORE_ADDRESS_VILLAGE", max_length=250, null=True, blank=True)
    STORE_ADDRESS_UNION = models.CharField("STORE_ADDRESS_UNION", max_length=250, null=True, blank=True)
    STORE_ADDRESS_THANA = models.CharField("STORE_ADDRESS_THANA", max_length=250, null=True, blank=True)
    STORE_ADDRESS_DISTRICT = models.CharField("STORE_ADDRESS_DISTRICT", max_length=250, null=True, blank=True)
    STORE_ADDRESS_DIVISION = models.CharField("STORE_ADDRESS_DIVISION", max_length=250, null=True, blank=True)
    STORE_EMPLOYEE_STATUS = models.BooleanField("EMPLOYEE_STATUS")
    STORE_NUMBER_OF_EMPLOYEE = models.IntegerField("NUMBER_OF_EMPLOYEE", null=True, blank=True)
    STORE_AVG_REVENUE = models.IntegerField("STORE_AVG_REVENUE", null=True, blank=True)
    STORE_AVG_COGS = models.IntegerField("STORE_AVG_COGS", null=True, blank=True)
    STORE_AVG_PROFIT = models.IntegerField("STORE_AVG_PROFIT", null=True, blank=True)
    STORE_CUSTOMER_CREDIT_LIMIT = models.IntegerField("CUSTOMER_CREDIT_LIMIT", null=True, blank=True)
    STORE_BUSINESS_TARGET = models.IntegerField("BUSINESS_TARGET", null=True, blank=True)
    STORE_REORDER_THRESHOLD = models.IntegerField("REORDER_THRESHOLD", null=True, blank=True)
    STORE_PASSWORD = models.TextField("STORE_PASSWORD", max_length=50, default=None, null=True, blank=True)

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

class MSE_EMPLOYEE(models.Model):
    emp_id = models.IntegerField(null=True, blank=True)
    store = models.ForeignKey(OWNER_STORE)
    MSE_EMPLOYEE_ID = models.IntegerField(null=True, blank=True)
    MSE_EMPLOYEE_NAME=models.CharField(max_length=250, null=True, blank=True)
    MSE_EMPLOYEE_MOBILE_NUMBER = models.CharField(max_length=250, null=True, blank=True)
    MSE_EMPLOYEE_JOINING_DATE = models.DateField(null=True, blank=True)
    MSE_EMPLOYEE_SALARY = models.IntegerField(null=True, blank=True)

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

对于我的serializers.py,我做了这个

class STORE_EMPLOYEE_SERIALIZER(serializers.ModelSerializer):
    employees = EMPLOYEE_SERIALIZER(many=True)
    class Meta:
        model = OWNER_STORE
        fields=("MSE_STORE_TYPE", "MSE_OWNER_INFO", "STORE_NAME", "STORE_ID", "STORE_ADDRESS_HOUSE", "STORE_ADDRESS_ROAD",
            "STORE_ADDRESS_VILLAGE", "STORE_ADDRESS_UNION", "STORE_ADDRESS_THANA", "STORE_ADDRESS_DISTRICT",
            "STORE_ADDRESS_DIVISION", "STORE_EMPLOYEE_STATUS", "STORE_NUMBER_OF_EMPLOYEE",
            "STORE_AVG_REVENUE", "STORE_AVG_COGS", "STORE_AVG_PROFIT", "STORE_CUSTOMER_CREDIT_LIMIT",
            "STORE_BUSINESS_TARGET", "STORE_REORDER_THRESHOLD", "STORE_PASSWORD", "employees")

我希望通过get请求,我会让所有员工都在商店,但我一直收到这个错误。

  

AttributeError:'OWNER_STORE'对象没有属性'employees'

并在终端的最后一行

  

AttributeError:尝试在序列化程序employees上获取字段STORE_EMPLOYEE_SERIALIZER的值时获得AttributeError。   序列化程序字段可能名称不正确,并且与OWNER_STORE实例上的任何属性或键都不匹配。   原始异常文本是:'OWNER_STORE'对象没有属性'employees'。

1 个答案:

答案 0 :(得分:0)

您需要使用 MSE_EMPLOYEE_set代替员工。 或者将related_name添加到store = models.ForeignKey(OWNER_STORE)

store = models.ForeignKey(OWNER_STORE, related_name='employees')