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
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
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/56577759.7020104%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.