1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to filter the one-to-many relationship from both side

Discussão em 'Python' iniciado por Stack, Setembro 10, 2024.

  1. Stack

    Stack Membro Participativo

    I have two tables which has relationship.

    class Parent(SafeDeleteModel):
    name = models.CharField(max_length=1048,null=True,blank=True)

    class Child(SafeDeleteModel):
    name = models.CharField(max_length=1048,null=True,blank=True)
    parent = models.ForeignKey(Parent,blank=True, null=True, on_delete=models.CASCADE,related_name="parent_project")


    In this case I can filter the Child by Parent such as

    Child.objects.filter(parent__name__contains="test")


    However I want to do the reverse like this below.

    Parent.objects.filter(child__name__contains="test")


    Is it possible?

    I tried like this below

    Parent.objects.filter(parent_project__contains="test")


    however this error

    django.core.exceptions.FieldError: Unsupported lookup 'contains' for ManyToOneRel or join on the field not permitted.

    Continue reading...

Compartilhe esta Página