Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join Multiple Querysets From Different Base Models Django

I currently have two different models.

class Journal(models.Model):
    date = models.DateField()
    from_account = models.ForeignKey(Account,related_name='transferred_from')
    to_account = models.ForeignKey(Account,related_name='transferred_to')
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    memo = models.CharField(max_length=100,null=True,blank=True)

class Ledger(models.Model):
    date = models.DateField()
    bank_account = models.ForeignKey(EquityAccount,related_name='paid_from')
    account = models.ForeignKey(Account)
    amount = models.DecimalField(max_digits=8, decimal_places=2)
    name = models.ForeignKey(Party)
    memo = models.CharField(max_length=100,null=True,blank=True)

I am creating a report in a view and get the following error: Merging 'ValuesQuerySet' classes must involve the same values in each case.

What I'm trying to do is only pull out the fields that are common so I can concatenate both of them e.g.

def report(request):

    ledger = GeneralLedger.objects.values('account').annotate(total=Sum('amount'))
    journal = Journal.objects.values('from_account').annotate(total=Sum('amount'))
    report = ledger & journal
...

If I try to make them exactly the same to test e.g.

def report(request):

    ledger = GeneralLedger.objects.values('memo').annotate(total=Sum('amount'))
    journal = Journal.objects.values('memo').annotate(total=Sum('amount'))
    report = ledger & journal
...

I get this error: Cannot combine queries on two different base models.

Anyone know how this can be accomplished?

like image 594
Crazyconoli Avatar asked Jul 18 '11 11:07

Crazyconoli


People also ask

How do I combine Querysets in Django?

Use union operator for queryset | to take union of two queryset. If both queryset belongs to same model / single model than it is possible to combine querysets by using union operator. One other way to achieve combine operation between two queryset is to use itertools chain function.

How do I join models in Django?

Join can be done with select_related method: Django defines this function as Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object data when it executes its query.

What is Django Q?

Django Q is a native Django task queue, scheduler and worker application using Python multiprocessing.

What are Django models?

A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you're storing. Generally, each model maps to a single database table. The basics: Each model is a Python class that subclasses django.db.models.Model .


1 Answers

from itertools import chain
report = chain(ledger, journal)

Itertools for the win!

If you want to do an Union, you should convert these querysets into python set objects.

If it is possible to filter the queryset itself rightly, you should really do that!

like image 115
lprsd Avatar answered Oct 11 '22 09:10

lprsd