Create a queryset that automatically queries onetoone objects

36 Views Asked by At

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"

I'm trying to create a view that returns the full JSON object with all data. 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
    }

I can go ahead and manually do the queries, convert back and forth from json, and manually link to the querysets:

    app = MyApp.objects.all().first()
    sites = Site.objects.filter(id__in=(app.site_1.pk, app.site_2.pk, app.site_3.pk))
    # look up sites and attach data
    serializer = JSONSerializer()
    app_data = serializer.serialize([app])
    site_data = serializer.serialize(sites)

    data_dict = json.loads(app_data)
    data_dict[0]["fields"]["site_1"] = json.loads(site_data)[0]
    data_dict[0]["fields"]["site_2"] = json.loads(site_data)[1]
    data_dict[0]["fields"]["site_3"] = json.loads(site_data)[2]
    app_data = json.dumps(data_dict)
    return HttpResponse(app_data, content_type="application/json")

and this gives me the result that I'm looking for.

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?

1

There are 1 best solutions below

1
parsarezaee On

You can do this:

def my_app_data(request):
queryset = MyApp.objects.prefetch_related('site1', 'site2', 'site3').all()

# Serialize queryset to JSON
data = serialize('json', queryset)

# Return JSON response
return JsonResponse(data, safe=False)

but if you wanna write serializer classes for your site and myapp model, check document: https://www.django-rest-framework.org/tutorial/1-serialization/