Search code examples
djangoregexdjango-modelsdjango-querysetdjango-filter

Django custom QuerySet with custom filter


The elements that are stored in Django model is as following

class Transcript(models.Model):
    text = models.CharField(max_length=10000, default="")
    confidence = models.FloatField(default=0)

And example data is as following :

text = "Word1(1.00-1.50) Word2(1.50-2.15) Word3(2.40-2.80) Word4(3.10-3.25) Word5(3.44-4.12) Word6(4.55-5.12) Word7(6.00-7.00) Word8(7.34-8.00)"
Transcript.object.create(text=text, confidence=99)

.

Transcript.objects.filter(text = "Word2 Word3")

Result : Object found -> Word2(1.50-2.15) Word3(2.40-2.80)

Transcript.objects.filter(text = "Word5 Word6 Word7 Word8")

Result : Object found -> Word5(3.44-4.12) Word6(4.55-5.12) Word7(6.00-7.00) Word8(7.34-8.00)

Transcript.objects.filter(text = "Word3 Word5")

Result : Object found - >Word3(2.40-2.80) Word4(3.10-3.25) Word5(3.44-4.12)

Transcript.objects.filter(text = "Word10 Word9")

Result : Object not found

How can I make these queries using filters and possibly using regex ?


Solution

  • You can try:

    # case sensitive:
    Transcript.objects.filter(text__contains = "Word1")
    
    # case insensitive 
    Transcript.objects.filter(text__icontains = "Word1")
    

    You can combine arguments to look for a transcript with "Word1" and "Word2" like:

    Transcript.objects.filter(
        text__contains = "Word1",
        text__contains = "Word2"
    )
    

    Details: https://docs.djangoproject.com/en/3.1/topics/db/search/