开发者

Django stacked inline admin question

开发者 https://www.devze.com 2023-02-09 19:33 出处:网络
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 ha

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开发者_JAVA百科 obj related information, but it's request argument seems to always be None when used in an inline. Is there another way?


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号