【问题标题】:Got AttributeError when attempting to get a value for field '' on serializer ''尝试获取序列化程序 \'\' 上字段 \'\' 的值时出现 AttributeError
【发布时间】:2022-08-11 19:24:01
【问题描述】:

我希望能够查看客户帐户。如您所见,Accounts 具有 Customer 的外键。 这个想法是能够看到带有嵌套帐户对象的客户信息,但它给了我一个错误

Got AttributeError when attempting to get a value for field `accounts_items` on serializer `CustomerSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Customer` instance.
Original exception text was: \'Customer\' object has no attribute \'Account\'.

就像这个例子一样,艺术家对象中的歌曲:

####MODELS####
class Account(models.Model):
    amount = models.FloatField(null=True)
    name = models.CharField(max_length=40)
    account_type = models.CharField(max_length=11, choices = ACCOUNT_TYPES)
    customer_id = models.ForeignKey(Customer, on_delete = models.CASCADE, default = None)
    bank_id = models.ForeignKey(Bank, on_delete = models.CASCADE, default = None)

    def __str__(self):
        return \'{} {} {} {} {}\'.format(self.amount, self.name, self.account_type, self.customer_id, self.bank_id)


class Customer(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=20)
    last_name = models.CharField(max_length=40)
    phone = models.CharField(max_length=40)
    email = models.EmailField(max_length = 100)
    rank = models.CharField(max_length=11, choices = RANK_TYPES)
    bank_id = models.ForeignKey(Bank, on_delete = models.CASCADE, default = None)

    def __str__(self):
        return \'{} {} {} {} {} {} {}\'.format(self.id, self.first_name, self.last_name, self.phone, self.email, self.rank, self.bank_id)

###VIEW###
class customer_page(generics.RetrieveUpdateDestroyAPIView):
    queryset = Customer.objects.all()
    serializer_class = CustomerSerializer

###SERIALIZER###
class AccountSerializer (serializers.ModelSerializer):

    class Meta:
        fields = (\'id\', \'amount\', \'name\', \'account_type\', \'customer_id\', \'bank_id\')
        model = Account


class CustomerSerializer (serializers.ModelSerializer):
    accounts_items = AccountSerializer(source=\'Account\')

    class Meta:
        fields = (\'id\', \'user\', \'first_name\', \'last_name\', \'phone\', \'email\', \'rank\', \'bank_id\', \'accounts_items\')
        model = Customer

    标签: django django-rest-framework django-serializer


    【解决方案1】:

    您需要将序列化程序更改为:

     
        class AccountSerializer (serializers.ModelSerializer):
        
            class Meta:
                fields = ('id', 'amount', 'name', 'account_type', 'customer_id', 'bank_id')
                model = Account
        
        
        class CustomerSerializer (serializers.ModelSerializer):
            accounts_items = serializers.SerializerMethodField()
        
            class Meta:
                fields = ('id', 'user', 'first_name', 'last_name', 'phone', 'email', 'rank', 'bank_id', 'accounts_items')
                model = Customer
        
            def get_accounts_items(self, obj):
                customer_account_query = models.Account.objects.filter(
                    customer_id=obj.id)
                serializer = AccountSerializer(customer_account_query, many=True)
        
                return serializer.data
    
    

    这应该工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      • 2022-11-29
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多