[Django]-How to filter with "contains"?



Why not do

baseSet = ThreadedComment.objects.filter(tree_path__contains = ('%010i' % int(baseT.comment_ptr_id)))

so that the search string for id=1 will be β€œ0000000001” and won’t be a substring of β€œ0000000011”?

EDIT: As per the comment below, it might be better to use COMMENT_PATH_DIGITS. This is a little messier because you’re using formatting to set a formatting tag. It looks like this:

tree_path__contains = ('%%0%ii' % COMMENT_PATH_DIGITS % int(baseT.comment_ptr_id))
πŸ‘€Paul Eastlund


the regexp would be '(^|/)0*%d(/|$)' % baseT.comment_ptr_id and you use it with tree_path__regex

read about MPTT for alternatives to this approach.


Leave a comment