Q:

Django only get object if one of it's subobjects meets condition

So I have two models:
class Business(models.Model):
    def __str__(self):
        return self.name

    name = models.CharField(max_length=200)

class Appointment(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    business = models.ForeignKey(Business, on_delete=models.CASCADE, related_name="appointments")

And in my view I have the following context:
def home(request):
    [...]
    context = {
        'business':Business.objects.order_by('name'),
    }
    [...]

Now I would get all businesses there are with their submodels "Appointment".
But what I want is only businesses where on of the existing submodels "Appointment" fullfillsauthor == request.author
Also the submodels "Appointment" of a Business should only be the "Appointments" where their author equals request.author

A:

We can do something like this:
business = Business.objects.filter(appointment__author=request.author)

Or:
business = Business.objects.filter(appointment__author__id=request.author.id)

You might want to read: Lookups that span relationships

