Skip to main content
 首页 » 编程设计

python之django smart_selects之syncdb 错误

2025年05月04日37langtianya

出现错误:

TypeError: init() 得到了一个意外的关键字参数 'chained_model_field'

在更改我的模型后运行 syncdb 时。

模型.py

class Category(models.Model): 
    cat_no = models.IntegerField(null=True, blank=True) 
    cat_txt = models.CharField(max_length=45) 
 
class E_cat(models.Model): 
    cat_no = models.ForeignKey(Category) 
    cat_txt = models.CharField(max_length=45) 
    scat_no = models.IntegerField(null=True, blank=True) 
    scat_txt = models.CharField(max_length=45) 
 
class Equip(models.Model): 
    category = models.ForeignKey(Category) 
    subcategory = models.ForeignKey( 
        E_cat, 
        chained_field="cat_no", 
        chained_model_field="cat_no", 
        show_all=False, 
        auto_choose=True, 
    ) 
    manufacturer = models.CharField(max_length=35, blank=True) 
    mfg_no = models.CharField(max_length=35, blank=True) 
    size = models.CharField(max_length=35, blank=True) 
    color = models.CharField(max_length=35, blank=True) 
    quanity = models.IntegerField(null=True, blank=True) 
    short_description = models.CharField(max_length=80, blank=True) 
    location_zip = models.IntegerField(null=True, blank=True) 
    listings = models.ForeignKey(Listings) 
    info = models.TextField(null=True, blank=True) 

请您参考如下方法:

这里的问题是 django.db.models.ForeignKeyField 不接受 chained_model_field 参数。

您可以在收到的回溯中看到这一点:

  File ".../models.py", line 42, in MyModel 
    subcategory = m.ForeignKey(…)               <--- here 
  File "…/related.py", line 840, in __init__ 
    Field.__init__(self, **kwargs) 
TypeError: __init__() got an unexpected keyword argument 'chained_model_field' 

可能你想要 https://github.com/digi604/django-smart-selects它定义了一个类似于 ForeignKey 的字段,它包含一个 chained_model_field 参数?