Создайте набор запросов, который автоматически запрашивает отдельные объекты.Python

Программы на Python
Anonymous
Создайте набор запросов, который автоматически запрашивает отдельные объекты.

Сообщение Anonymous »


I'm building a django application that has a model with multiple OneToOne relationships:

class MyApp(BaseModel): site_1 = models.OneToOneField( Site, on_delete=models.CASCADE, related_name="site1", null=True ) site_2 = models.OneToOneField( Site, on_delete=models.CASCADE, related_name="site2", null=True ) site_3 = models.OneToOneField( Site, on_delete=models.CASCADE, related_name="site3", null=True ) def __str__(self): return self.title class Meta: verbose_name = "Main Application" verbose_name_plural = "Main Application" When I create a queryset using MyApp.objects.all() it returns an object with just the primary keys:

{ "model": "mesoamerica.mesoamericaapp", "pk": 1, "fields": { "site_1": 1, "site_2": 2, "site_3": 3 } However, I'd like to create a queryset that grabs each of the Site models by pk and attaches them to the query, rather than having to do that myself. Is there a queryset that will do this?


Источник: https://stackoverflow.com/questions/780 ... ne-objects

Вернуться в «Python»