-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 Hi,
On Tue, Oct 09, 2018 at 08:03:27AM -0700, megaman wrote: > 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? The syntax that you suggested here, unfortunately, is not valid Python. In an argument list, you can use either positional arguments, which are either Python expressions, or keyword arguments, but those require a valid Python identifier to be on the left side, which “question_text.contains” is not. > Basically, can I do the same thing without adding “magic strings” to > field names? Not at the moment, sorry. The idea of using a style more similar to SQLAlchemy has been floated on the dev mailing list in the past (as in, years ago), but I cannot even find the right thread at the moment, and I'm not aware of any recent developments in this direction. All in all, chaining relationships, expressions, and filters with __ in keyword arguments is how this is done in Django, and it's most likely going to stay that way for the foreseeable future. Michal -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJbvb0CAAoJEHA7T/IPM/klndQP/Rd91q/4ANlPzkIzzJd3xRuX xrJgvFnWNnv9tZdQK1kwaoQPRG2Msx9taahg2NPvVMQUNKbsMKW60dctmtUMpXVC MXrWscLEwXecV94Zp10x9JFPJCMLjNvDELvWjkLIMz7/j/Ih4h5FnzfHTiwlg4DK 2XkGiuBaXVM+TKST8RI1w7YphvcjfdehQzy1wRx8wS5FoqgrBjxJ4FJJojDYeo5s MO0kfPLzPHg4QIU6eYwg2euSmACUorkrXVj0q1Qr8b5AsAkVCPVIM7ryapGrorp4 6EZ+97FMwemoCAtkWzL6jSvnnp87Mm/587BzxRTOJ0ff2pkUv5wjizTmED5/Hur9 fmjz4CpuVx2wHsvt0ty4gNyQ693h2J1kRZxenP0iFAnzipRPy/Lll46uxB3MhX5P uHjM+gqk6RvHfFh4H/jBe2mh2WSEKtpCPG5kRp3v/cw6qgV16CeQDk60u8Vlymqk SXj0FGj9W/HGytVt11+tGKX0fPcnDyy4GF5CIRBVTfviq/yz+GZQcO1Dd2VoAaMc fAwLpKsPNQrTnJBZXPSby/bgYTYljyIBl6YOe9heL4yCocQVEO6zQRmZ81hCzB4H 9DoMiW6Y7oILcm7L/DC8DY/if2aLyxSN4Rnl05yPZnAmiatIMxssW5rWrz4+7YiS iH62Ro8wxl48vrSxC7Hu =gsXH -----END PGP SIGNATURE----- -- 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/20181010084907.GF18928%40koniiiik.org. For more options, visit https://groups.google.com/d/optout.

