Django Tastypie:加入两个模特

时间:2015-10-25 11:27:29

标签: python django tastypie

我是django和tastypie的新手。我想加入两个模型,即order和order_item,根据订单ID从两个模型中获取数据。我从几个来源引用但仍未在两个模型中显示数据。

订单型号:

user = models.ForeignKey(USER_MODEL, blank=True, null=True,
            verbose_name=_('User'))
    status = models.IntegerField(choices=STATUS_CODES, default=PROCESSING,
            verbose_name=_('Status'))
    order_subtotal = CurrencyField(verbose_name=_('Order subtotal'))
    order_total = CurrencyField(verbose_name=_('Order Total'))
    shipping_address_text = models.TextField(_('Shipping address'), blank=True,
        null=True)
    billing_address_text = models.TextField(_('Billing address'), blank=True,
        null=True)
    created = models.DateTimeField(auto_now_add=True,
            verbose_name=_('Created'))
    modified = models.DateTimeField(auto_now=True,
            verbose_name=_('Updated'))
    cart_pk = models.PositiveIntegerField(_('Cart primary key'), blank=True, null=True)
    userid_consgeo = models.TextField(blank=True, null=True)
    cartid_consgeo = models.TextField(blank=True, null=True)

OrderItem'模型:

order = models.ForeignKey(get_model_string('Order'), related_name='items',
            verbose_name=_('Order'))
    layer_reference = models.CharField(max_length=255,
            verbose_name=_('Layer reference'))
    layer_name = models.CharField(max_length=255, null=True, blank=True,
            verbose_name=_('Layer name'))
    layer = models.ForeignKey((ResourceBase),
            null=True, blank=True, **f_kwargs)
    unit_price = CurrencyField(verbose_name=_('Unit price'))
    quantity = models.DecimalField(verbose_name=_('Quantity'), default=0.00, max_digits=12, decimal_places=2)
    line_subtotal = CurrencyField(verbose_name=_('Line subtotal'))
    line_total = CurrencyField(verbose_name=_('Line total'))

我的资源:

class OrderItemResources(ModelResource):
    order_id = fields.CharField(attribute='order_id')
    layer_id = fields.CharField(attribute='layer_id')
    all_order = fields.ToManyField('resources.JoinOrderResources', 'all_order_set', related_name = 'all_order', full=True)

    class Meta:
        queryset = OrderItem.objects.all()
        resource_name = 'order_item'

        filtering = {
            "layer_id": ALL_WITH_RELATIONS,
            "order_id": ALL_WITH_RELATIONS,
        }

class JoinOrderResources(ModelResource):
    id = fields.CharField(attribute='id')
    user = fields.CharField(attribute='user')
    order_id = fields.ForeignKey(OrderItemResources, 'order_item', full=True, null=True)
    class Meta:
        queryset = Order.objects.all()
        resource_name = 'all_order'
        list_allowed_methods = ['get']
        authorization = Authorization()

        filtering = {
           "id": ALL,
           "user": ALL_WITH_RELATIONS,
        }

1 个答案:

答案 0 :(得分:0)

您在OrderItem-Model中设置了related_name='items'。所以你想要做的是:

class OrderResource(ModelResource):
     id = fields.CharField(attribute='id')
     user = fields.CharField(attribute='user')
     # order_id = fields.ForeignKey(OrderItemResources, 'order_item', full=True, null=True)
     items = fields.ToManyField(OrderItemResources, 'items', full=True, null=True)


    class Meta:
        queryset = Order.objects.all()
        resource_name = 'order_item'

    filtering = {
        "id": ALL,
        "user": ALL_WITH_RELATIONS,
    }