简体   繁体   中英

Compare objects from two models in Django

I have two models PriceActual and PriceBenchmark having fields date and price.

I want to compare the actual prices with the benchmark prices.

I'm not interested in benchmark prices with dates which is not present in the actual prices. So if PriceActual only has objects from the last week, I only want to query objects from PriceBenchmark also within the last week.

I guess it's something like

actual = PriceActual.objects.all()
benchmark = PriceActual.objects.filter(date__in=actual)

Edit

The models are really simple

class PriceActual(models.Model):
    date = DateField()
    price = DecimalField()

class PriceBenchmark(models.Model):
    date = DateField()
    price = DecimalField()

Maybe not the most efficient...but you could always create a list of dates:

actual = PriceActual.objects.all()
actual_dates = [x.date for x in actual]
benchmark = PriceActual.objects.filter(date__in=actual_dates)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM