Django - How to restrict foreign key choices in dropdown menu depending on datatime field

后端 未结 1 923
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-26 01:48


I have form for adding data in database and I want to restrict foreign key choices in my dropdown menu. It have to be restricted on my finishDate field. If finishDate is dat

相关标签:
1条回答
  • 2021-01-26 02:07

    You can filter it in the __init__

    import datetime
    
    def NetForm(forms.ModelForm):
        #...fields
        def __init__(self, *args, **kwargs):
            super(NetForm, self).__init__(*args, **kwargs)
            # Now set the queryset...
            self.fields['idAccount'].queryset = Account.objects.filter(finishDate__gte=datetime.datetime.now())
    
    0 讨论(0)
提交回复
热议问题