Merge querysets from different django models

If you were in a situation where you need to merge two querysets from different models into one, you’ve surely see this error:

Cannot combine queries on two different base models.

The solution is to use itertools.chain which make an iterator that is the junction of the given iterators.

from itertools import chain

result_lst = list(chain(queryset1, queryset2))

Now, you can sort the resulting list by any common field, e.g. creation date

from itertools import chain
from operator import attrgetter

result_lst = sorted(
    chain(queryset1, queryset2),
    key=attrgetter('created_at'))
Advertisements

May Day

Hey! It’s May day! I’m looking for something relaxing me. Ok, let’s do cheesecake.

Ingredients

For the base:

  • 150g butter
  • 400g biscuits (SAIDA)

For the filling:

  • 50g sugar
  • 3 eggs
  • 500g ricotta
  • 1 tbsp vanilla extract
  • 300ml pot double cream
  • 2 pot petits-suisse

To decorate:

  • whipped cream
  • lemon juice
  • Kiwi

Method

  1. Melt the butter in a pan. Crush the biscuits by bashing them in a bag with a rolling pin, then stir them into the butter until very well coated. Press the mixture firmly into the base of the tin and chill.IMG_20150501_150958
  2. Put the eggs and the sugar into a bowl and, using an electric whisk, beat for a few secs until smooth.
  3. Add the vanilla extract, the ricotta, the double cream and petits-suisse and mix well.
  4. Tip the filling onto the biscuit base.
  5. Surround the mold with aluminum foil.IMG_20150501_151135
  6. Put he mold into an other  larger mold filled with water.IMG_20150501_151345
  7. Put them into the oven in 140° C during 1h15min.IMG_20150501_151509
  8. Let it cool downIMG_20150501_165745

Good appetite 🙂

IMG_20150501_173122