我在让django“智能选择”应用程序正常运行时遇到问题。它不会填充我的链式字段的选择。 github“https://github.com/digi604/django-smart-selects”上的“自述”文件提供了以下说明。
我发现这些说明有点神秘,而且他们没有显示Continent和County表的模型,以便更容易。
我之前发布了一个有关此事的问题,我没有得到回应。它有关于我的特定型号的更多信息。 “http://stackoverflow.com/questions/9155350/django-smart-selects-second-level-wont-populate”
希望有一些“django-smart-choices”专家可以让我超越驼峰。感谢
from smart_selects.db_fields import ChainedForeignKey
class Location(models.Model)
continent = models.ForeignKey(Continent)
country = ChainedForeignKey(
Country,
chained_field="continent",
chained_model_field="continent",
show_all=False,
auto_choose=True
)
area = ChainedForeignKey(Area, chained_field="country", chained_model_field="country")
city = models.CharField(max_length=50)
street = models.CharField(max_length=100)
答案 0 :(得分:2)
你应该在urls.py中插入它:
url(r'^ chaining /',include('smart_selects.urls')),
这就是我所做的和工作:
#=================================
#models.py
from smart_selects.db_fields import ChainedForeignKey
class Continent(models.Model):
continent = models.CharField(max_length = 45)
def __unicode__(self):
return unicode(self.continent)
class Country(models.Model):
continent = models.ForeignKey(Continent)
country = models.CharField(max_length = 45)
def __unicode__(self):
return unicode(self.country)
class Area(models.Model):
country = models.ForeignKey(Country)
area = models.CharField(max_length = 45)
def __unicode__(self):
return unicode(self.area)
class Location(models.Model):
continent = models.ForeignKey(Continent)
country = ChainedForeignKey
(
Country,
chained_field = "continent",
chained_model_field = "continent",
show_all = False, auto_choose = True
)
area = ChainedForeignKey(Area, chained_field = "country", chained_model_field = "country")
city = models.CharField(max_length = 50)
street = models.CharField(max_length = 100)
#=================================
#admin.py
from yourapp.models import Continent
from yourapp.models import Country
from yourapp.models import Area
from yourapp.models import Location
class ContinentAdmin(admin.ModelAdmin):
pass
class CountryAdmin(admin.ModelAdmin):
pass
class AreaAdmin(admin.ModelAdmin):
pass
class LocationAdmin(admin.ModelAdmin):
pass
admin.site.register(Continent, ContinentAdmin)
admin.site.register(Country, CountryAdmin)
admin.site.register(Area, AreaAdmin)
admin.site.register(Location, LocationAdmin)
答案 1 :(得分:0)
在country = ChainedForeignKey()字段的给定示例中,chained_field =“continent”指的是Location类中的“continent”字段。 chained_model_field =“continent”指的是Country模型中的“continent”字段(示例中未显示)。
答案 2 :(得分:0)
这就是我将其解释为dragonx的方式。但我显然错过了一些东西。
我认为其他类看起来像这样
class Continent(models.Model):
continent = models.CharField(max_length=45)
def __unicode__(self):
return self.continent
class Country(models.Model):
continent = models.ForeignKey(Continent)
country = models.CharField(max_length=45)
def __unicode__(self):
return self.county