2011-05-03 56 views
0

我有一个模型,带有一个名为Country的模型的外键。如何通过用户属性过滤管理选项?

每次我在管理员中编辑我的模型时,在选择国家选项时,我会看到很多国家。我希望该选项列表可以通过用户属性(例如user.get_profile()。continent)进行预过滤。

我可以在哪里挂钩?

感谢

回答

0

检查http://docs.djangoproject.com/en/dev/ref/contrib/admin/ - “ModelAdmin.formfield_for_choice_field()”

class MyModelAdmin(admin.ModelAdmin): 
    def formfield_for_choice_field(self, db_field, request, **kwargs): 
     if db_field.name == "country": 
      kwargs['choices'] = get_country_choices_for_continent(request.user.get_profile().continent) 
     return super(MyModelAdmin, self).formfield_for_choice_field(db_field, request, **kwargs)