Search code examples
djangodjango-querysettastypie

django tastypie resource queryset filter doesn't work if resource is pulled into another


Models

class Quote(models.model):
    quote_ref = models.TextField(null=True, blank=True)
    order = models.Foreignkey('Order', related_name='quotes')
    version models.DecimalField(null=True, blank=True)
    requested_date = models.DateField(null=True, blank=True)
    expiry_date = models.DateField(null=True, blank=True)
    closed_date = models.DateField(null=True, blank=True)

class Order(models.model):
    order_ref = models.CharField(null=True, blank=True)
    cost models.DecimalField(null=True, blank=True)
    order_date = models.DateField(null=True, blank=True)
    delivery_date = models.DateField(null=True, blank=True)
    .......
    .......

Resources

class RequestsResource(ModelResource):
    quotes = fields.ToManyField('api.resources.QuoteIndexResource', 'quotes', full=True, null=True)
    class Meta:
        queryset = Order.objects.all()
        resource_name = 'request'


class QuoteIndexResource(ModelResource):

    class Meta:
        queryset = Quote.objects.all().filter(closed_date__isnull=True)
        resource_name = 'index_quote'

If I use the QuoteIndexResource on its own the filter on the querysetworks but if it is pulled into RequestsResource then the filter doesn't have any effect on the data.

Is there a way to make the .filter(closed_date__isnull=True) work in this scenario?


Solution

  • My bad I should have closed this question when I fixed it. so I found if I used attribute=lambda bundle I would get errors. but using just lambda bundle: It all works ok.

    quotes = fields.ToManyField(
        'api.resources.QuoteIndexResource', lambda bundle: Quote.objects.filter(order=bundle.obj, closed_date__isnull=True),
        full=True, null=True
    

    )