Increasing the searching speed of Django set.all()

Viewed 25

I am using Django to filter through a MongoDB full of data. Right now I have code that is working and have found where my slowdown is at...

def does_this_property_apply(prop, app, dev, event):

    for platform_device in prop.platformdevice_set.all():
        for event in platform_device.applicable_events.all():
            if event.name == event:
                for app in platform_device.applicable_app.all():
                    if app.name == app:
                        for device in platform_device.applicable_device.all():
                            if device.name == dev:
                                print("This property applies")

Basically I have 400+ properties that have a bunch of metadata. Inside of each property is a platform_device object (model) that contains information such as app, dev, and event. Think of it as this property only applies to certain applications, on certain devices, on certain user actions (events). Each property can have multiple of these.

I am new to Django and trying to find out how I could quickly search all platform_device in the database and find if this property (prop) is applicable to my event, device, and application.

class PlatformDevice(models.Model):
    _id = models.UUIDField(unique=True, primary_key=True,default=uuid.uuid4, editable=False)
    name = 'Property Specifics'

    property_field = models.ForeignKey('Property', on_delete=models.CASCADE)

    applicable_app = models.ArrayReferenceField(to=Platform, on_delete=models.CASCADE, blank=True)
    applicable_device = models.ArrayReferenceField(to=Device, on_delete=models.CASCADE, blank=True)
    applicable_events = models.ArrayReferenceField(to=Event, on_delete=models.CASCADE, blank=True)
    event_group = models.ArrayReferenceField(to=EventGroups, on_delete=models.CASCADE, blank=True)

    def __str__(self):
        return self.name
0 Answers
Related