Hello!

In general, I will explain the situation first:

I have a model

class Friends(models.Model, MixinModel): by = models.ForeignKey(Profile, related_name="from_friend") friend = models.ForeignKey(Profile, related_name="to_friend") approve = models.CharField(max_length=2, choices=RELATION_STATUS, default=State.No) 

It is an intermediate model for linking Profile and Profile. In the inside of the ListView profiles, I override the get_queryset (it turns out, this is the queryset of profiles) by the approve parameter of the intermediate model and the current user. I didn’t think of anything except filtering by

f = Friends.object.filter (approve = State.No)

, get from this filter the id-swords of the by field and compare them with the id-schnics of the default queryset.

Can this be done somehow more beautifully and less costly? It seems to me that there should be a way to filter models according to an intermediate model. after all, they invented it?

    1 answer 1

    You can filter through reverse relationships.

    https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships

    If I correctly understood your case, then something like this:

    friends = Profile.objects.filter (friends__approve = State.No,)

    The only thing you need to create m2m connection (suppose, calling friends) on self in the profile and specify through the table. Hope to help.

    If anything, here in Elvish is + - a suitable example) https://stackoverflow.com/questions/12891302/filtering-many-to-many-relationship-by-relationship-field-in-django

    • Thanks, the link example gave me Cannot resolve keyword 'friends' into field . But by some miracle it worked like this: Profile.objects.filter (to_friend__approve = State.No) - digital-mag