[Answer]-Django queryset counts substrings in charField


results = MyModel.objects.all()

pos_id = 0

values_for_pos_id = [res.field_to_check.split('-')[pos_id] for res in results]
values_for_pos_id = set(values_for_pos_id)

How does this work:

  • first you fetch all your objects (results)
  • pos_id is your substring index (you have 4 substring, so it’s in range 0 to 3)
  • you split each field_to_check (aka: where you store the substring combinations) on - (your separator) and fetch the correct substring for that object
  • you convert the list to a set (to have all the unique values)

Then a simple len(values_for_pos_id) will do the trick for you

NB: If you don’t have pos_id or can’t set it anywhere, you just need to loop like this:

for pos_id in range(4):
    values_for_pos_id = set([res.field_to_check.split('-')[pos_id] for res in results])
    # process your set results now
    print len(values_for_pos_id)


Try something like this…

# Assumes your model name is NumberStrings and attribute numbers stores the string.
search_string = "1-1-2-1"
matched_number_strings = NumberStrings.objects.filter(numbers__contains=search_string)
num_of_occurrences = len(matches_found)
matched_ids = [match.id for match in matched_number_strings]


You could loop through these items (I guess they’re strings), and add the value of each substring_n to a Set_n.

Since set values are unique, you would have a set, called Set_1, for example, that contains 1,2,3,10,11.

Make sense?

👤Rob L

Leave a comment