1
cars = Car.objects.distinct().filter(mileage__gte=500000) \
.exclude(visit__date__gte=time_threshold)
For your real models code should look like this:
samochody = Samochod.objects.distinct().filter(przebieg__gte=500000) \
.exclude(wizyta__data__gte=time_threshold)
Source:stackexchange.com