1

My models.py looks something like

class RelevanceRelation(TimeStampable, SoftDeletable, models.Model):
    relevance_type = models.ForeignKey(
        RelevanceType,
        on_delete=models.CASCADE,
        related_name="relevance_relations"
    )
    name = models.CharField(max_length=256,
                            verbose_name="Relevance Relation Name")

    def __str__(self):
        return self.name


class RelevanceRelationValue(TimeStampable, SoftDeletable, models.Model):
    entity = models.ForeignKey(
        Entity, on_delete=models.CASCADE,
        related_name="relevance_relation_values"
    )
    relevance_relation = models.ForeignKey(
        RelevanceRelation,
        on_delete=models.CASCADE,
        related_name="values"
    )
    name = models.CharField(max_length=256,
                             verbose_name="Relevance Relation Value")

    def __str__(self):
        return self.name

And I have two querysets

q1 = RelevanceRelationValue.objects.filter(entity=<int>)
q2 = RelevanceRelation.objects.filter(relevance_type=<int>)

Now is there a way to find intersection of q1 and q2 i.e I wan't to display all the values of q2 whose id is present in q1 as rulevance_relation

For example:

q1 = -------------------------------
     | entity | relevance_relation |
     -------------------------------
     |   1    |        1           |
     |   1    |        2           |
     |   1    |        3           |
     -------------------------------

and q2 = -------------------------------
         |   id.  | relevance_type     |
         -------------------------------
         |   1    |        1           |
         |   2    |        1           |
         |   3    |        1           |
         |   4    |        1           |
         |   5    |        1           |
         |   6    |        1           |
         ------------------------------- 

so q3 should be 
         -------------------------------
         |   id.  | relevance_type     |
         -------------------------------
         |   1    |        1           |
         |   2    |        1           |
         |   3    |        1           |
         ------------------------------- 

1 Answer 1

1

You can perform extra filtering:

q1 = RelevanceRelationValue.objects.filter(entity=some_value1).values('relevance_relation')
q2 = RelevanceRelation.objects.filter(
    relevance_type=some_value2,
    id__in=q1
)

But it makes more sense to simply filter on the related model, so:

RelevanceRelation.objects.filter(
    values__entity=some_value1,
    relevance_type=some_value2
).distinct()

Here we thus get all RelevanceRelations for which the relevance_type is some_value2, and for which a related RelevanceRelationValue exists with entity=some_value1.

Sign up to request clarification or add additional context in comments.

3 Comments

I am getting an error Original exception text was: 'RelevanceRelation' object has no attribute 'entity_type'.
@Jack: well that is correct, a RelevanceRelation has indeed no attriubte .entity_type, so likely further in you code you aim to obtain attributes that do not exist for the given model.
oh yes I was calling wrong serialiser and Thank You for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.