Django annotation using data from another table without relations

Viewed 58

What I'm trying to do is annotate a queryset with information from a few tables that don't have foreign key relations between them, but can be identified from values in the main table. I realise that I could add relations and do something like this, but the db I'm using doesn't allow for relations.

So given models:

class DailyShopData(models.Model):
    shop_id = models.IntegerField()
    currency = models.CharField()
    date = models.DateField()
    amount = models.IntegerField()

    class Meta:
        unique_together = ('shop_id', 'date', 'currency')

class DailyUserData(models.Model):
    shop_id = models.IntegerField()
    currency = models.CharField()
    date = models.DateField()
    unique_users = models.IntegerField()

    class Meta:
        unique_together = ('shop_id', 'date', 'currency')

We can relate the two fields via the ('shop_id', 'data_date', 'currency') key.

So what I'd like to do for my annotation is:

daily_shop_data_queryset.objects.values('shop_id').annotate(
    shop_amount=Sum("amount"),
    shop_unique_users=Sum(???)  
)

So that the result would be aggregated amount and unique_users per shop_id.

0 Answers
Related