Django smart_selects 链式字段空了
Django smart_selects chained field coming up empty
我正在尝试使用 Django smart_selects 实现链式下拉列表。但它没有按预期工作。选择第一个下拉列表后,第二个下拉列表未填充。
model.py:
class ServerInfo(models.Model):
server = models.CharField(max_length=255, verbose_name='Server')
def __str__(self):
return self.server
class Database(models.Model):
database = models.CharField(max_length=255, verbose_name='Database')
server = models.ForeignKey(ServerInfo,related_name='database', on_delete=models.CASCADE)
def __str__(self):
return self.database
class Tables(models.Model):
table = models.CharField(max_length=255, verbose_name='Table')
database = models.ForeignKey(Database,related_name='table', on_delete=models.CASCADE)
def __str__(self):
return self.table
class TransferJob(models.Model):
source_server = models.ForeignKey(ServerInfo, related_name='source_server', verbose_name='Source Server', on_delete=models.CASCADE)
source_db = ChainedForeignKey(
Database,
chained_field="source_server",
chained_model_field="server",
show_all=False,
auto_choose=True,
sort=True)
source_table = models.ForeignKey(Tables, related_name='source_table',on_delete=models.CASCADE, verbose_name='Source Table')
dest_server = models.ForeignKey(ServerInfo, related_name='destination_server', verbose_name='Destination Server', on_delete=models.CASCADE)
dest_db = models.ForeignKey(Database, related_name='destination_database', verbose_name='Destination Database', on_delete=models.CASCADE)
更新:
我刚刚意识到这在 django 管理员中按预期工作。但是我如何在管理员之外使用它?
能够通过在模板中添加这些脚本来解决此问题:
<script type="text/javascript" src="{% static '/smart-selects/admin/js/chainedfk.js' %}"></script>
<script type="text/javascript" src="{% static '/smart-selects/admin/js/bindfields.js' %}"></script>
文档中没有提到这些。
我正在尝试使用 Django smart_selects 实现链式下拉列表。但它没有按预期工作。选择第一个下拉列表后,第二个下拉列表未填充。
model.py:
class ServerInfo(models.Model):
server = models.CharField(max_length=255, verbose_name='Server')
def __str__(self):
return self.server
class Database(models.Model):
database = models.CharField(max_length=255, verbose_name='Database')
server = models.ForeignKey(ServerInfo,related_name='database', on_delete=models.CASCADE)
def __str__(self):
return self.database
class Tables(models.Model):
table = models.CharField(max_length=255, verbose_name='Table')
database = models.ForeignKey(Database,related_name='table', on_delete=models.CASCADE)
def __str__(self):
return self.table
class TransferJob(models.Model):
source_server = models.ForeignKey(ServerInfo, related_name='source_server', verbose_name='Source Server', on_delete=models.CASCADE)
source_db = ChainedForeignKey(
Database,
chained_field="source_server",
chained_model_field="server",
show_all=False,
auto_choose=True,
sort=True)
source_table = models.ForeignKey(Tables, related_name='source_table',on_delete=models.CASCADE, verbose_name='Source Table')
dest_server = models.ForeignKey(ServerInfo, related_name='destination_server', verbose_name='Destination Server', on_delete=models.CASCADE)
dest_db = models.ForeignKey(Database, related_name='destination_database', verbose_name='Destination Database', on_delete=models.CASCADE)
更新: 我刚刚意识到这在 django 管理员中按预期工作。但是我如何在管理员之外使用它?
能够通过在模板中添加这些脚本来解决此问题:
<script type="text/javascript" src="{% static '/smart-selects/admin/js/chainedfk.js' %}"></script>
<script type="text/javascript" src="{% static '/smart-selects/admin/js/bindfields.js' %}"></script>
文档中没有提到这些。