That's interesting because I would have thought it's much better to chain through functions like in Javascript or in other functional paradigms. Chaining using the variable name feels "fragile" to me, like I may break things at any time without knowing.
Maybe I will just have to get used to how Python and Django work. I'm still very new to both of these. :P On Wednesday, 10 October 2018 00:08:10 UTC+8, Matthew Pava wrote: > > No. > Usually, you would try to keep all of your filtering in the managers > module, and then you would be able to limit your refactoring of filters in > that file. > You could try using *args and **kwargs syntax, but that would make it > difficult to maintain. > I suppose you could functions in SQLAlchemy, and if you really wanted > functions in Django, you could make them yourself. > > I actually like the Django syntax. It's very easy to follow. You can > also use the __ for related fields. > Questions.objects.filter(something__related_something__related_something_else__lower__contains="what") > > > > > -----Original Message----- > From: [email protected] <javascript:> [mailto: > [email protected] <javascript:>] On Behalf Of megaman > Sent: Tuesday, October 9, 2018 10:03 AM > To: Django users > Subject: Alternatives to using __contains? > > I’m pretty new to Django. I have just learnt that could filter a query set > this way: > > Questions.objects.filter(question_text__contains=‘what’) > > This kind of scares me a little because the filter “contains” is actually > part of the field name. And I guess there are other operators appended to a > field with the same __operation pattern. > > Wouldn’t this make refactoring tricky? Even renaming the field name has to > be done carefully. In my mind, this feels messy. > > Is there an alternative to doing this? Something like: > Questions.objects.filter(question_text.contains=‘what’) > Or using “contains” as a function? > > Basically, can I do the same thing without adding “magic strings” to field > names? > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected] <javascript:>. > To post to this group, send email to [email protected] > <javascript:>. > Visit this group at https://groups.google.com/group/django-users. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/3f7194e9-b600-4a14-be1e-83f0cb35dac6%40googlegroups.com. > > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/74f4d731-3b9f-4c73-ab33-41eb76c0f26e%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.

