gpt4 book ai didi

python - django python 中的 Search_fields

转载 作者:太空狗 更新时间:2023-10-29 21:17:57 28 4
gpt4 key购买 nike

我想知道如何使用外键来执行搜索

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)

比在我的 admin.py 文件中有

from CMS.Inventory.models import Product

class padmin(admin.ModelAdmin):
search_fields=['name', 'description', 'code', 'lot_no' ]
admin.site.register(Product, padmin)

但我希望“LotComments”能够使用与“Product”相同的搜索字段作为代码等。

希望我解释得很好

最佳答案

您可以在管理 search_fields 中指定相关字段搜索,就像在 Django 查询集上一样。检查 documentation .对于 LotComments 对象,search_fields 看起来像这样:

search_fields = ['product_lot__product__name', 
'product_lot__product__description',
'product_lot__product__code',
'product_lot__lot_no']

关于python - django python 中的 Search_fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2106084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com