Filter by foreign key property in django-nerel

What is the best practice for filtering based on a foreign key property in a non-relational database? I understand that the lack of support joincomplicates the situation, and therefore I was wondering how others get around it.

In my case, I have events belonging to sites belonging to regions. I want to filter out all the events in this region. Eventhas a property sitethat is a foreign key for site, which, in turn, has a foreign key regionfor region:

region = Region.objects.get(id=regionID)
events = Event.objects.filter(site__region=region)

This does not work because the __region site is required joinand which is not supported in the django-nerel running in the Google App Engine. (I get it Caught DatabaseError while rendering: This query is not supported by the database.as an error.) I thus repeat the events, adding those that match the list:

events = list()
region = Region.objects.get(id=regionID)
for event in Event.object.all():
    if event.site.region==region:
        events.append(event)

Is this a good way to do something? Is there something stupid that I forgot? Thanks in advance!

+3
source share
2 answers

This is a very inefficient solution because you are looking for a site and region, causing a lot of queries. This will not exceed 100 events in your database.

, . Event on save(). Event.objects.filter(region_id = regionID). , , , .

, : , .;)

+8

, dbindexer JOINS. : http://www.allbuttonspressed.com/blog/django/joins-for-nosql-databases-via-django-dbindexer-first-steps

dbindexer, , - :

# photo/dbindexes.py:

from models import Event
from dbindexer.lookups import StandardLookup
from dbindexer.api import register_index

register_index(Event, {'site__region': StandardLookup(),})
+1

Source: https://habr.com/ru/post/1782351/


All Articles