我一直想为依赖的下拉列表实现Django Smart-Selects。 我写了https://github.com/digi604/django-smart-selects上的代码 但是第二个字段没有填充 这是该项目的模型:
#型号:
class ObjectName(models.Model):
objectName=models.CharField(max_length=100)
objFullForm=models.CharField(max_length=100, default='')
def __str__(self):
return self.objectName
class ActionObject(models.Model):
objectName=models.ForeignKey(ObjectName,on_delete=models.CASCADE)
action=models.CharField(max_length=100)
def __str__(self):
return self.action
class Priority(models.Model):
priorityName=models.CharField(max_length=100)
priotityScore=models.IntegerField()
def __str__(self):
return self.priorityName
from smart_selects.db_fields import ChainedForeignKey
class ActionObjectPriority(models.Model):
objectName=models.ForeignKey(ObjectName, on_delete=models.CASCADE)
actionName = ChainedForeignKey(
ActionObject,
chained_field="objectName",
chained_model_field="objectName",
show_all=False,
auto_choose=True,
sort=True)
priority=models.ForeignKey(Priority, on_delete=models.CASCADE)
Base.html模板:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Base</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script> -->
{% load static %}
<script src="{% static 'smart-selects/admin/js/chainedfk.js' %}"></script>
<script src="{% static 'smart-selects/admin/js/chainedm2m.js' %}"></script>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
优先级模板:
{% extends 'base.html' %}
{% block body %}
<div class="container">
<h1>Add Priority</h1>
<form method="POST" class="post-form form-control">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="save btn btn-primary">Save</button>
</form>
</div>
{% endblock %}
未填充“动作”字段。 Form