formfield_for_foreignkey difficulty

155 views
Skip to first unread message

Mike Dewhirst

unread,
Nov 26, 2015, 7:25:26 AM11/26/15
to django...@googlegroups.com
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.

The m2m relation is substance<-m2m->substance where any substance can be
an ingredient in a mixture (also a substance).

The through table is ...

class Substance_Ingredients(models.Model):
substance = models.ForeignKey('Substance', null=True, blank=True,
related_name='base_substance')
ingredient = models.ForeignKey('Substance', null=True, blank=True,)
proportion = models.DecimalField(null=True, blank=True,)

Is that a limitation in the admin or should I try harder?

Thanks

Mike

Mike Dewhirst

unread,
Nov 26, 2015, 4:20:24 PM11/26/15
to django...@googlegroups.com
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)

BTW, ingredient in Substance_Ingredients (see below) is a substance,
Substance has a foreign key to Division which in turn has a foreign key
to Company

Mike Dewhirst

unread,
Nov 26, 2015, 4:39:50 PM11/26/15
to django...@googlegroups.com
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)

Mike Dewhirst

unread,
Nov 26, 2015, 5:07:45 PM11/26/15
to django...@googlegroups.com
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
to add an ingredient to make a new mixture the drop-down list is empty.

I tried replacing the kwargs stanza with this ...

kwargs["queryset"] = Substance.objects.filter(
Q(substance__division__company=company))

... which ought to work but no luck.

Any ideas?

Thanks

Mike
Reply all
Reply to author
Forward
0 new messages