How do I filter ForeignKey choices in a Django ModelForm?

前端 未结 7 1928
感情败类
感情败类 2020-11-22 09:07

Say I have the following in my models.py:

class Company(models.Model):
   name = ...

class Rate(models.Model):
   company = models.ForeignKey(C         


        
7条回答
  •  半阙折子戏
    2020-11-22 09:39

    If you haven't created the form and want to change the queryset you can do:

    formmodel.base_fields['myfield'].queryset = MyModel.objects.filter(...)
    

    This is pretty useful when you are using generic views!

提交回复
热议问题