bounty将在5天后过期。回答此问题可获得+50的声誉奖励。Anee Mes正在寻找来自声誉良好来源的答案。
所以我尝试为django 1.11.4实现django-smart-selects 1.5.9,但不知何故,链接键无法正常工作。
class Province(models.Model):
name = models.CharField(
max_length=30
)
class Meta:
verbose_name = _("province")
verbose_name_plural = _("provinces")
def __str__(self):
return self.name
class District(models.Model):
province = models.ForeignKey(
Province,
on_delete=models.CASCADE
)
name = models.CharField(
max_length=50
)
class Meta:
verbose_name = _("district")
verbose_name_plural = _("districts")
def __str__(self):
return self.name
class City(models.Model):
district = models.ForeignKey(
District,
on_delete=models.CASCADE
)
name = models.CharField(
max_length=50
)
class Meta:
verbose_name = _("city")
verbose_name_plural = _("cities")
def __str__(self):
return self.name
class Ward(models.Model):
city = models.ForeignKey(
City,
on_delete=models.CASCADE
)
name = models.CharField(
max_length=2
)
class Meta:
verbose_name = _("ward")
verbose_name_plural = _("wards")
def __str__(self):
return self.name
class School(models.Model):
# other fields .....
province = models.ForeignKey(
Province,
on_delete=models.SET_NULL,
blank=True,
null = True,
verbose_name = _("province")
)
district = ChainedForeignKey(
District,
chained_field="province",
chained_model_field="province",
show_all=False,
)
city = ChainedForeignKey(
City,
chained_field="district",
chained_model_field="district",
show_all=False,
)
ward = ChainedForeignKey(
Ward,
chained_field="city",
chained_model_field="city",
show_all=False,
)
urls.py
url(r'^admin/', admin.site.urls),
url(r'^admin/', include('smart_selects.urls')),
admin.py
@admin.register(School)
class SchoolAdmin(admin.ModelAdmin):
inlines = [ServerInline, ServerUpdateInline]
list_display = ['school_name', 'school_type','phone',
'province', 'district', 'city', 'ward',
]
search_fields = ('school_name','district__name')
list_filter = ('school_type', 'district')
在这里尝试了从django-smart-selects链接下拉实现模型和管理员是在一个单独的应用程序称为学校和我提供的URL是在基地django目录的url.py
1条答案
按热度按时间slsn1g291#
您当前的URL是
我检查了docs,他们使用
chaining
而不是admin/
。你能试着按照他们的文档看看是否有效吗?