django orm:在限制字段上过滤
在我的应用中,我有一个文档编号,该文档号由文档模型的几个字段组成,例如:
{{doc_code}}{{doc_num}}-{{doc_year}}
doc_num是模型中的整数,但是对于用户来说,它是一个五位数字符串,其中空的空格被零填充,例如00024,例如00024,或00573。DOC_YEAR
是模型中的日期字段,但在完整的文档编号中,这是一年的最后两个数字。
因此,对于用户,文档编号为-TR123.00043-22。
我想在“文档列表”页面上实现搜索。
一种方法是在文档模型的保存方法中从doc_code,doc_num和doc_year字段中自动化full_number字段,并在此full_number上过滤。
在查询之前使用过滤器之前,Anothe将使用Concat功能。 首先,由Full_Code字段contectinate Full_code字段
docs = Document.annotate(full_code=Concat('doc_code', 'doc_num', Value('-'), 'doc_year', output_field=CharField()))
,而是通过Full_Code字段进行过滤,
docs = docs.filter(full_code__icontain=keyword)
但是如何将DOC_NUM作为五位数字字符串和DOC_YEAR作为concat函数的五位数字字符串和doc_year作为concat函数?
还是有什么更好的解决方案来解决此任务?
In my app, I have a document number which consists of several fields of Document model like:
{{doc_code}}{{doc_num}}-{{doc_year}}
doc_num is an integer in the model, but for the user, it is a five digits string, where empty spaces are filled by zero, like 00024, or 00573.
doc_year is a date field in the model, but in full document number, it is the two last digits of the year.
So for users, the document number is for example - TR123.00043-22.
I want to implement searching on the documents list page.
One approach is to autogenerate the full_number field from doc_code, doc_num and doc_year fields in the save method of Document model and filter on this full_number.
Anothe is to use Concat function before using of filter on query.
First by concatinate full_code field
docs = Document.annotate(full_code=Concat('doc_code', 'doc_num', Value('-'), 'doc_year', output_field=CharField()))
and than filter by full_code field
docs = docs.filter(full_code__icontain=keyword)
But how to pass doc_num as five digits string and doc_year as two last digits of year to Concat function?
Or what could be a better solution for this task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Concat只会采用字段名称和字符串值,因此您实际上没有很多我知道的选项。
如您指出的那样,您可以在保存上设置额外的字段。如果您要在多个地方使用它,那可能是最好的方法。
保存功能看起来会看起来像ike
doc_num需要python> = 3.6,可以看到其他pythons的其他方法这里
doc_year认为它是dateTime类型。如果仅是四位数int,那么诸如str(doc_year)[-2:]之类的东西应该起作用。
或者,如果您只使用它很少使用它,则可以循环循环添加一个额外的字段
,然后将其转换为列表,这样您就不会进行另一个DB调用并丢失新字段,并通过列表理解过滤它。
将filtered_docs传递到模板中,然后离开。
Concat will only take field names and string values, so you don't really have many options there that I know of.
As you note, you can set an extra field on save. That's probably the best approach if you are going to be using it in multiple places.
The save function would look something ike
doc_num requires python>= 3.6, other methods for earlier pythons can be seen here
doc_year assumes it is a datetime type. If it is just a four digit int then something like str(doc_year)[-2:] should work instead.
Alternately, if you are only ever going to use it rarely you could loop through your recordset adding an additional field
and then convert it to a list so you don't make another DB call and lose your new field, and filter it via list comprehension.
pass filtered_docs to your template and away you go.