gpt4 book ai didi

python - 从 django admin 添加实例

转载 作者:行者123 更新时间:2023-11-28 18:19:18 24 4
gpt4 key购买 nike

这就是我的类(class)的样子。

class School(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = models.ForeignKey(Region, on_delete=models.CASCADE)
cluster = models.ForeignKey(Cluster, on_delete=models.CASCADE)
school_name = models.CharField(max_length=250)
facilitator = models.CharField(max_length=250)
f_number = models.IntegerField()
f_email = models.EmailField()
school_logo = models.FileField(default='')
school_strength = models.IntegerField()

在哪个州,地区,集群也是类。我试图创建一个学校网络,分为 3 个级别,即州、地区、集群。我假设当我从 django admin 添加学校时,我认为如果我选择状态和过滤集群,它可能会过滤区域,当我选择区域时,它会很容易。但这似乎不起作用。虽然在选择特定的州和地区并在集群下拉列表中添加学校后,所有州和地区的所有集群都会出现。

是否有针对此问题的解决方案或目前我们在 django 中没有这样的选项?

最佳答案

尝试来自 django-smart-selectsChainedForeignKey打包它就像变魔术一样。

安装smart_selects

pip install django-smart-selects

项目设置.py

INSTALLED_APPS=[
. . . . .
. . . . .
'smart_selects',
. . . . .
]

项目 urls.py

urlpatterns = [
. . . . .
url(r'^admin/', include(admin.site.urls)),
url(r'^chaining/', include('smart_selects.urls')),
. . . . .
]

参见 installation of django-smart-selects

ChainedForeignKey 的工作

from smart_selects.db_fields import *

class State(models.Model):
stateName = models.CharField(max_length=500)
. . . .

class Region(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
regionName = models.CharField(max_length=500)
. . . . .
. . . . .


class Cluster(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = ChainedForeignKey(Region,chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
clusterName = models.CharField(max_length=500)
. . . . .
. . . . .



class school(models.Model):
state = models.ForeignKey(State, on_delete=models.CASCADE)
region = ChainedForeignKey(Region, chained_field="state",chained_model_field="state", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
cluster = ChainedForeignKey(Cluster, chained_field="region",chained_model_field="region", show_all=False, auto_choose=True, sort=False, on_delete=models.CASCADE)
schoolName = models.CharField(max_length=500)
. . . .
. . . .

试试这个,它在 admin area 中运行得很好。

关于python - 从 django admin 添加实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46244199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com