DJANGO模型:是否有可能使用经理使用特定字段上的特定字段来强制IContains?
假设我有一个与此类似的字符串字段的模型,
class Product(models.Model):
upc = models.CharField(max_length=12, blank=False, null=False)
这是可以构建模型管理器或类似的模型,而每次我使用product.objects.get
/product.objects.filter时
它覆盖默认查找,以强迫搜索/过滤器的行为,就像我使用过icontains
一样?
如下:
Product.objects.get(upc="012345678902")
Product.objects.filter(upc="012345678902")
默认情况下的行为会如下:
Product.objects.get(upc__icontains="012345678902")
Product.objects.filter(upc__icontains="012345678902")
Let's say I have a model with a string field similar to this
class Product(models.Model):
upc = models.CharField(max_length=12, blank=False, null=False)
Is it possible to build a model manager or something similar where every time I use Product.objects.get
/Product.objects.filter
it overrides the default lookup to force the search/filter to behave like I had used icontains
?
As in:
Product.objects.get(upc="012345678902")
Product.objects.filter(upc="012345678902")
By default would behave like:
Product.objects.get(upc__icontains="012345678902")
Product.objects.filter(upc__icontains="012345678902")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以自定义查询管理器或QuerySet,并覆盖GET/过滤器方法。下面的演示可以告诉您如何自定义查询管理器实施强制IContains。
更自定义可以看到 https:/ 1400/custom-managers and QuerySets
QueryManager在QuerySet中使用
baseManager.from_queryset
的所有方法代理所有方法,选择一个已经足够了。You can custom a query manager or queryset, and override the get/filter method. The demo below can tell you how to custom a query manager implements force icontains.
More customize can see https://sodocumentation.net/django/topic/1400/custom-managers-and-querysets
QueryManager has proxy all method in QuerySet by
BaseManager.from_queryset
, choose one is enought.