On 27/11/2015 8:38 AM, Mike Dewhirst wrote:
> On 27/11/2015 8:19 AM, Mike Dewhirst wrote:
>> On 26/11/2015 11:24 PM, Mike Dewhirst wrote:
>>> I have formfield_for_foreignkey working for a couple of fields in my
>>> substance model but find a brick wall when I try to restrict choices in
>>> an inline m2m foreign key.
>>
>> It was late last night and I forgot to mention Django 1.8.7 Admin,
>> Python 3.4 and 2.7 running on Windows 8.1 and Ubuntu 12.04 and 14.04.
>>
>> In the admin looking at a substance, Ingredients can be added
>> successfully from a drop-down list but I'm trying to restrict choices in
>> that list. Here is my failing attempt in IngredientsInline() ...
>>
>> def formfield_for_foreignkey(self, db_field, request, **kwargs):
>> """Limit choices for 'ingredient' field """
>> company = get_user_company(request)
>> if
db_field.name == 'ingredient':
>> kwargs["queryset"] = Substance_Ingredients.objects.filter(
>> Q(ingredient__division__company=company))
>> return super(SubstanceAdmin, self).formfield_for_foreignkey(
>> db_field, request, **kwargs)
>
> The last line was wrong. This works ... (Yay! :)
>
> return super(SubstanceAdmin.IngredientsInline,
> self).formfield_for_foreignkey(db_field, request, **kwargs)
It doesn't work at all. It only shows existing ingredients. When I try
... which ought to work but no luck.