问题描述
我想知道是否有人会返回与选择选项中选择的会话相关的类别。
示例:
class Session(models.Model):
名称= models.CharField(Sessão,max_length = 100)
slug =模型。 SlugField( Slug,max_length = 100)
类Category(models.Model):
session = models.ForeignKey(Session,verbose_name ='Sessão',on_delete = models.CASCADE, max_length = 100,默认值= 1,空白= True,null = True)
category = models.CharField( Categoria,max_length = 100)
slug = models.SlugField( Slug,max_length = 100)
class Article(models.Model):
category = models.ForeignKey(Category,verbose_name ='Categoria',on_delete = models.CASCADE,max_length = 100,default = 1, blank = True,null = True)
session = models.ForeignKey(Session,verbose_name ='Sessão',on_delete = models.CASCADE,max_length = 100,default = 1,blank = True,null = True)
当r例如,撰写一篇文章,我将为该文章定义一个会话,然后我想返回类别,仅返回与该会话相关的类别。
有人可以帮助我吗?
我相信这必须是动态的...
强烈的拥抱!
第一个解决方案:
覆盖模板(可以帮助您)
字段更改时,您需要对API进行ajax调用,该API带有session_id并返回所有类别。通过JS将返回的结果填充到类别中。
此外,您可以尝试
第二个解决方案:(不使用JS / AJAX)
要注册文章,您可以先进入会话管理员,然后在其中添加文章的链接。
a。
如果要执行changeform_view操作,则
您可以使用内联管理并覆盖其形式init()来预填充类别。
b 。
如果要在changelist_view中链接,
http:// localhost:8000 / admin /< app> / article / add /?session_id =<>
您可以覆盖Article Admin表单init()并读取传递的session_id并相应地使用它。 / p>
I wonder if anyone would know a way to return the categories related to the session chosen in the select options.
Example:
class Session(models.Model):
name = models.CharField("Sessão", max_length=100)
slug = models.SlugField("Slug", max_length=100)
class Category(models.Model):
session = models.ForeignKey(Session, verbose_name='Sessão', on_delete=models.CASCADE, max_length=100, default=1, blank=True, null=True)
category = models.CharField("Categoria", max_length=100)
slug = models.SlugField("Slug", max_length=100)
class Article(models.Model):
category = models.ForeignKey(Category, verbose_name='Categoria', on_delete=models.CASCADE, max_length=100, default=1, blank=True, null=True)
session = models.ForeignKey(Session, verbose_name='Sessão', on_delete=models.CASCADE, max_length=100, default=1, blank=True, null=True)
When registering an Article, I would define a session for the article, and then I would like to return categories, only the categories related to that session.
Can anybody help me ?I believe this would have to be dynamically ...
Strong hug!
1st solution:
Override the template (Django Documentation can help you with that)
Onchange of the field, you need to make an ajax call to an API which takes in session_id and returns you all the categories. Fill the returned result into categories through JS.
Also, you can give a try to django-ajax-selects
2nd solution: (without JS/AJAX)
To register an article, You can first go to session admin and then have a link there to add an article.
a.If you want to do it changeform_view then,You can use inline admin and override its form init() to prepopulate the categories.
b.If you want the link in changelist_view,
http://localhost:8000/admin/<app>/article/add/?session_id=<>
You can override the Article Admin form init() and read the session_id passed and use it accordingly.
这篇关于限制与选择中所选对象相关的对象的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!