django admin formfield for manytomany not working

时间:2013-02-19 01:37:06

标签: python django

我正在读这个

https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_manytomany

我试图用like field查询dtabase但是我收到错误

  

FieldError:不允许加入字段'car'。你是否   misspell'like'用于查找类型?

我的代码是

def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "cars":
            kwargs["queryset"] = models.Cars.objects.filter(car_like="ford")
        return super(MyAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

1 个答案:

答案 0 :(得分:0)

发布的代码不会抛出此错误。如果您使用双下划线car__like,它只会混淆连接。 You should check out the django query reference for exact details.

无论如何,perhaps you are looking for __icontains生成一个LIKE语句?

Foo.objects.filter(some_field__icontains='ford')