【问题标题】:Remove selection value from duplicate ForeignKey if already selected in the first如果已在第一个中选择,则从重复的 ForeignKey 中删除选择值
【发布时间】:2015-11-02 05:35:15
【问题描述】:

我有型号Category:

class Category(models.Model):
    name = models.CharField("Category Name", max_length=120, default='', null=True, blank=True)

还有模特Organization

class Organization(models.Model):
    name = models.CharField("Organization name", max_length=120, default='')
    m_primary_category = models.ForeignKey('Category', null=True, blank=True, default='', related_name="+", verbose_name='Primary Category')
    m_secondary_category = models.ForeignKey('Category', null=True, blank=True, default='', related_name="+", verbose_name='Secondary Category')

用户必须首先添加类别,然后在组织模型中选择主和次级字段。

问题是:如果选择已经在主要选择中,如何从次要隐藏/删除类别?

示例: - 用户在类别模型中添加:Cat1、Cat2 和 Cat3。 - 用户添加:组织名称:Org,选择主要类别:Cat1。那么“次要类别”中的可用类别必须是:Cat2、Cat3。

【问题讨论】:

    标签: django django-models


    【解决方案1】:

    如果你想在不提交表单的情况下显示和隐藏元素,你需要一些 javascript,使用 jquery 这应该很容易

    $('#id_m_primary_category').change(function(){
        // etc
    });
    

    当然不能信任用户的输入,因此您仍然需要验证表单

    def clean(self):
        if self.cleaned_data.get('m_primary_category') == self.cleaned_data.get('m_secondary_category'):
            raise forms.ValidationError('No')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 2014-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      相关资源
      最近更新 更多