我想在limit_choices_to中使用外键,但是我收到错误
无法解析关键字u' product_type.product_type'进入田野。选项包括:id,pantvariation,product_brand,product_brand_id,product_id,product_type,product_type_id,shirtvariation
class ProductType(models.Model):
product_type = models.CharField(max_length=70, choices=choices.PRODUCT_TYPES)
variation_model_name = models.CharField(max_length=70,blank=True, null=True)
def save(self, *args, **kwargs):
temp = ProductType()
temp.product_type = self.product_type
temp.variation_model_name = self.product_type.capitalize() + "Variation"
super(ProductType, temp).save(*args, **kwargs)
def __unicode__(self):
return self.product_type
class WarehouseStock(models.Model):
product_id = models.CharField(max_length=120, unique=True)
product_type = models.ForeignKey(ProductType)
product_brand = models.ForeignKey(Brand)
def __unicode__(self):
return self.product_id
class PantVariation(models.Model):
product = models.ForeignKey(WarehouseStock, limit_choices_to= {'product_type.product_type': 'pant'})
size = models.CharField(max_length=3, choices= choices.PANT_SIZES)
colour = models.CharField(max_length=10)
fit = models.CharField(max_length=10, choices=choices.FIT_TYPES)
我想在limits_choices_to中使用WarehouseStock中的product_type.product_type,我该怎么做?
编辑:完整错误:
/ admin / warehouse / pantvariation / add /中的FieldError 无法解析关键字u" product_type.product_type'进入田野。选项包括:id,pantvariation,product_brand,product_brand_id,product_id,product_type,product_type_id,shirtvariation
请求方法:GET
请求网址:http://127.0.0.1:8000/admin/warehouse/pantvariation/add/
Django版本:1.9.7
异常类型:FieldError
例外值:无法解析关键字u" product_type.product_type' 进入田野。选择是:id,pantvariation,product_brand, product_brand_id,product_id,product_type,product_type_id, shirtvariation
答案 0 :(得分:0)
恕我直言,你不能那么容易地做这项工作。但是,我会以这种方式重构您的PantVariation模型:
class PantVariation(models.Model):
product = models.ForeignKey(ProductType, related_name="pants", limit_choices_to= {'product_type.': 'pant'}
)
那应该有用。您仍然可以通过ProductType模型访问WarehouseStock模型。这样做你想要的吗?