I
I
i3a4em2022-03-27 04:28:26
Django
i3a4em, 2022-03-27 04:28:26

How to get all fields of related models cascading?

Lord comrades! Tell me, is there any elegant way to make a request to the database?

class Projects(models.Model):
    name = models.CharField(max_length=32, default=None, null=True, verbose_name="Название")

class Clusters(models.Model):
    project = models.ForeignKey(Projects, null=True, on_delete=models.CASCADE)

class Accounts(models.Model):
    clusters = models.ForeignKey(Clusters, null=True, on_delete=models.SET_NULL)


How should I make a query to get a QuerySet with all project accounts? To be honest, I don’t even know how to formulate this to ask Google ((

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2022-03-27
@i3a4em

it's basically basic

Accounts.objects.filter(clusters__project__name='Имя проекта')
And the models are usually called in the singular.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question