filter foreignkey field in django admin

后端 未结 7 1297
感情败类
感情败类 2020-12-02 20:42

I have these models:

class Entity(models.Model):
       name=models.CharField(max_length=100)
      
class Theme(models.Model):
   name=models.CharField(max_l         


        
相关标签:
7条回答
  • 2020-12-02 21:13

    I actually prefer to do it in get_form like so:

    class CompanyAdmin(admin.ModelAdmin):
        def get_form(self, request, obj=None, **kwargs):
            form = super(CompanyAdmin, self).get_form(request, obj, **kwargs)
            form.fields['theme'].queryset = Theme.objects.filter(name__iexact='company')
            return form
    
    0 讨论(0)
提交回复
热议问题