Opened 9 years ago

Closed 9 years ago

#26100 closed Uncategorized (worksforme)

Queryset.extra use case: iregex against regular expressions stored as fields

Reported by: Jeff Johnson Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords: Queryset.extra
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As requested, here is an example of something I couldn't figure out how to do without using Queryset.extra().

Given an email address, find all regular expressions from the database that match the email address. It would be like doing iregex in reverse.

class EmailFilter(models.Model):
    regex = models.CharField(blank=False, max_length=128, db_index=True, help_text="Do not send to email addresses that match this regular expression.  Not case sensitive.  E.g.: ^admin@.*$")

    @classmethod
    def find_matching_rules(cls, email):
        return cls.objects.extra(where=['%s ~* regex'], params=[email])

Change History (1)

comment:1 by Tim Graham, 9 years ago

Resolution: worksforme
Status: newclosed

I believe that should be straightforward to implement using the expressions API. Please see TicketClosingReasons/UseSupportChannels if you need help converting it and reopen this ticket if there's some reason I'm missing why that won't work. Thanks.

Note: See TracTickets for help on using tickets.
Back to Top