python中search,django python中的search_fields
发布日期:2022-03-15 11:49:53 浏览次数:24 分类:技术文章

本文共 1820 字,大约阅读时间需要 6 分钟。

I was wondering how I am able to use a foreign key to preform a search for example

class Product(models.Model):

name = models.CharField(max_length = 127)

description = models.TextField()

code = models.CharField(max_length = 127)

def __unicode__(self):

return self.name + " - " + self.code

class ProductLot(models.Model):

product = models.ForeignKey(Product)

code = models.CharField(max_length = 30)

lot_no = models.CharField(max_length = 30)

location = models.CharField(max_length = 127)

incoming = models.IntegerField()

commited = models.IntegerField()

available = models.IntegerField()

reorder = models.IntegerField()

created_date = models.DateField(auto_now_add=True)

def __unicode__(self):

return self.code + " - " + self.product.name + " - " + self.lot_no

class LotComment(models.Model):

product_lot = models.ForeignKey(ProductLot)

comment_user = models.ForeignKey(User, null=True)

comment_text = models.TextField()

created_date = models.DateField(auto_now_add=True)

def __unicode__(self):

return self.product_lot.product.code + " - " +

self.product_lot.product.name + " - " +

self.product_lot.lot_no + " - " + str(self.created_date)

than in my admin.py file I have

from CMS.Inventory.models import Product

class padmin(admin.ModelAdmin):

search_fields=['name', 'description', 'code', 'lot_no' ]

admin.site.register(Product, padmin)

but I want for 'LotComments' to be able to use the same search fields that 'Product' can as for code ect.

Hope I explained this well

解决方案

You can specify related field searches in the admin search_fields the same way you do on Django querysets. Check on the documentation. For the LotComments object, the search_fields would look something like:

search_fields = ['product_lot__product__name',

'product_lot__product__description',

'product_lot__product__code',

'product_lot__lot_no']

转载地址:https://blog.csdn.net/weixin_34574003/article/details/118901760 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:冯偌依曼计算机的基本原理是,03级计算机专业《计算机组成原理》试卷A
下一篇:restclient发送json,如何使用javax.ws.rs.client.WebTarget从REST客户端发送json对象

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月22日 10时24分16秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

表格相关的html语言,HTML标记语言——表格标记 2019-04-21
web聊天界面html,PC端Web聊天界面+代码分享(HTML+CSS) 2019-04-21
cmake qt 添加路径 项目_CMake配置Qt工程 2019-04-21
使用python开发的软件协议_WEB开发——Python WSGI协议详解 2019-04-21
冰点下载器手机版apk_冰点文库下载器 2019-04-21
python信号采集代码_13行代码实现:Python实时视频采集(附源码) 2019-04-21
h5引入json_纯js直接引入json文件 2019-04-21
python格式化字符串总结_Python字符串处理方法总结 2019-04-21
python中true什么意思_python中的bool是什么意思 2019-04-21
case是不是php语言关键字,PHP语言 switch 的一个注意点 2019-04-21
config.php渗透,phpMyAdmin 渗透利用总结 2019-04-21
java list 合并 重复的数据_Java ArrayList合并并删除重复数据3种方法 2019-04-21
android volley 上传图片 和参数,android - 使用android中的volley将图像上传到multipart中的服务器 - 堆栈内存溢出... 2019-04-21
android开发的取消清空按钮,Android开发实现带清空按钮的EditText示例 2019-04-21
android gp服务,ArcGIS Runtime SDK for Android开发之调用GP服务(异步调用) 2019-04-21
mysql整体会滚_滚mysql 2019-04-21
向mysql数据库中添加批量数据类型_使用JDBC在MySQL数据库中快速批量插入数据 2019-04-21
最全的mysql 5.7.13_最全的mysql 5.7.13 安装配置方法图文教程(linux) 强烈推荐! 2019-04-21
mssql连接mysql数据库文件_在本地 怎么远程连接MSSQL数据库 2019-04-21
mssql 远程无法连接mysql_解决SQLServer远程连接失败的问题 2019-04-21