1

我对 django-rest-framework 和嵌套对象有一些问题。

我有一个 Cart 对象以及 CartItem,它链接回一个 Cart:

class Cart(models.Model):
    customer = models.ForeignKey(Customer)
    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

class CartItem(models.Model):
    cart = models.ForeignKey(Cart, related_name='cartitems')
    product = models.ForeignKey(Product, help_text='Product in a cart')
    quantity = models.PositiveIntegerField(default=1, help_text='Quantity of this product.')
    date_added = models.DateTimeField(auto_now_add=True, help_text='Date that this product was added to the cart.')

我为两者创建了序列化程序:

class CartItemSerializer(serializers.ModelSerializer):
    product = serializers.HyperlinkedRelatedField(view_name='product-detail')

    class Meta:
        model = CartItem

class CartSerializer(serializers.ModelSerializer):
    customer = serializers.HyperlinkedRelatedField(view_name='customer-detail')
    cartitems = CartItemSerializer(required=False)
    total_price = serializers.CharField(source='total_price', read_only=True)
    shipping_cost = serializers.CharField(source='shipping_cost', read_only=True)

    class Meta:
        model = Cart
        fields = ('id', 'customer', 'date_created', 'date_modified', 'cartitems', 'total_price', 'shipping_cost')

但是,每当我尝试 POST 以创建新购物车时,我都会收到一个错误,假设它尝试设置不存在的 CartItem 时:

TypeError at /api/v1/carts/
add() argument after * must be a sequence, not NoneType

但是,购物车不需要实际拥有 CartItems。

有没有办法让 DRF 尊重required=False我在 Cart.cartitems 上获得的标志?

干杯,维克多

编辑:

我再次尝试追踪它:

它使用 CartSerializer 对象在 rest_framework/serializers.py 中调用 BaseSerializer.save()。

def save(self, **kwargs):
    """
    Save the deserialized object and return it.
    """
    if isinstance(self.object, list):
        [self.save_object(item, **kwargs) for item in self.object]

        if self.object._deleted:
            [self.delete_object(item) for item in self.object._deleted]
    else:
        self.save_object(self.object, **kwargs)

    return self.object

然后它在同一个类上调用 save_object() :

def save_object(self, obj, **kwargs):
    """
    Save the deserialized object and return it.
    """
    if getattr(obj, '_nested_forward_relations', None):
        # Nested relationships need to be saved before we can save the
        # parent instance.
        for field_name, sub_object in obj._nested_forward_relations.items():
            if sub_object:
                self.save_object(sub_object)
            setattr(obj, field_name, sub_object)

    obj.save(**kwargs)

    if getattr(obj, '_m2m_data', None):
        for accessor_name, object_list in obj._m2m_data.items():
            setattr(obj, accessor_name, object_list)
        del(obj._m2m_data)

    if getattr(obj, '_related_data', None):
        for accessor_name, related in obj._related_data.items():
            if isinstance(related, RelationsList):
                # Nested reverse fk relationship
                for related_item in related:
                    fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
                    setattr(related_item, fk_field, obj)
                    self.save_object(related_item)

                # Delete any removed objects
                if related._deleted:
                    [self.delete_object(item) for item in related._deleted]

            elif isinstance(related, models.Model):
                # Nested reverse one-one relationship
                fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name
                setattr(related, fk_field, obj)
                self.save_object(related)
            else:
                # Reverse FK or reverse one-one
                setattr(obj, accessor_name, related)
        del(obj._related_data)

Cart 对象有一个_related_data设置为 dict 的字段:

{'cartitems': None}

因此,在倒数第二行,它调用了 django/db/models/fields/related.py 中的 setattr:

def __set__(self, instance, value):
    if instance is None:
        raise AttributeError("Manager must be accessed via instance")

    manager = self.__get__(instance)
    # If the foreign key can support nulls, then completely clear the related set.
    # Otherwise, just move the named objects into the set.
    if self.related.field.null:
        manager.clear()
    manager.add(*value)

正是这最后一个班轮 (manager.add(*value)) 导致:

TypeError: add() argument after * must be a sequence, not NoneType
4

1 回答 1

1

检查Serializer Relation Docs,首先您需要添加many=True到您的cartitems字段。

不幸的是,这是只读的。文档只是说“对于读写关系,你应该使用扁平的关系样式”——你可以在这里找到一个关于这个的问题(尽管这只是处理 1-1 的情况)。

当前的策略包括使cartitems只读,然后:做某事post_save,使用第二个序列化程序或向单独的端点发出单独的请求以设置相关实体​​。鉴于对嵌套写入的更好支持即将到来,我目前可能倾向于向单独的端点发送单独的请求(尽管这显然取决于您的约束)。

我希望这会有所帮助。

编辑:(更新评论中的问题和讨论后)。

如果您使用单独的端点来添加 CartItems,那么将其设为cartitems只读应该可以消除该错误。

但是(如果您没有将其设为只读)查看您从中发布的 DRF 代码save_object会发现,在related_item in related您确实需要一个列表的块中。没有 CartItems 的 Cart 的适当 dict (fragment) is not {'cartitems': None}but rather {'cartitems': []}。— 这当然意味着你的required=False旗帜没有做任何事情。(所以也许简短的回答是“否” - <a href="https://groups.google.com/forum/#!topic/django-rest-framework/2iEat5mCbvY/discussion" rel="nofollow noreferrer">Will现在听邮件列表讨论

于 2013-08-27T08:44:29.437 回答