orokusaki
orokusaki

Reputation: 57198

Django stacked inline admin question

Using Django 1.2 I have a stacked inline admin with a many to many field on it. I need to limit the choices in the many to many based on the parent object that the inline exists for. For example, I have a WidgetPart inline that's on the Widget admin. When I'm editing an existing Widget I need to limit WidgetPart.foo choices based on logic pertaining to the Wiget that is being edited. I can't seem to do this with formfield_for_manytomany, as not only does it not provide any obj related information, but it's request argument seems to always be None when used in an inline. Is there another way?

Upvotes: 0

Views: 887

Answers (1)

Ben Belchak
Ben Belchak

Reputation: 549

You can do something like this on your InlineAdmin class:

def formfield_for_manytomany(self, db_field, request, **kwargs):
  if db_field.name == "foo":
    kwargs["queryset"] = SomeModel.objects.filter(something=something)
    return db_field.formfield(**kwargs)

  return super(YourModel, self).formfield_for_manytomany(db_field, request, **kwargs)

Upvotes: 1

Related Questions