我想知道如何使用外键来执行搜索,例如
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 查询集的方式相同。检查文档 https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields。为了LotComments
对象,即search_fields
看起来像:
search_fields = ['product_lot__product__name',
'product_lot__product__description',
'product_lot__product__code',
'product_lot__lot_no']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)