如何在Django Admin中双向访问ManyToManyField?

发布于 2024-10-05 01:44:02 字数 1102 浏览 2 评论 0原文

Django 管理 filter_horizo​​ntal 设置提供了一个很好的小部件,用于编辑多对多关系。但这是一个需要字段列表的特殊设置,因此它仅在定义 ManyToManyField 的(管理员)模型上可用;我怎样才能在(管理员的)其他模型上获得相同的小部件,向后读取关系?

我的模型看起来像这样(请随意忽略 User/UserProfile 复杂性;尽管这是真正的用例):

class Site(models.Model):
    pass
class UserProfile(models.Model):
    user = models.OneToOneField(to=User,unique=True)
    sites = models.ManyToManyField(Site,blank=True)

我可以在管理表单上获得一个不错的小部件 < code>UserProfile 与

filter_horizontal = ('sites',)

但无法看到如何在 Site 管理员上获取等效内容。

我还可以通过向 SiteAdmin 添加内联来获得部分方式,定义为:

class SiteAccessInline(admin_module.TabularInline):
    model = UserProfile.sites.through

虽然它是迂回且不方便的;该小部件对于简单管理多对多关系来说一点也不直观。

最后,有一个技巧此处描述,其中涉及定义另一个Site 上的 ManyToManyField 并确保它指向同一个数据库表(并跳过一些麻烦,因为 Django 并不是真正设计为在描述相同内容的不同模型上拥有不同字段数据)。我希望有人能给我展示一些更干净的东西。

The Django admin filter_horizontal setting gives a nice widget for editing a many-to-many relation. But it's a special setting that wants a list of fields, so it's only available on the (admin for the) model which defines the ManyToManyField; how can I get the same widget on the (admin for the) other model, reading the relationship backwards?

My models look like this (feel free to ignore the User/UserProfile complication; it's the real use case though):

class Site(models.Model):
    pass
class UserProfile(models.Model):
    user = models.OneToOneField(to=User,unique=True)
    sites = models.ManyToManyField(Site,blank=True)

I can get a nice widget on the admin form for UserProfile with

filter_horizontal = ('sites',)

but can't see how to get the equivalent on the Site admin.

I can also get part-way by adding an inline to SiteAdmin, defined as:

class SiteAccessInline(admin_module.TabularInline):
    model = UserProfile.sites.through

It's roundabout and unhandy though; the widget is not at all intuitive for simply managing the many-to-many relationship.

Finally, there's a trick described here which involves defining another ManyToManyField on Site and making sure it points to the same database table (and jumping through some hoops because Django isn't really designed to have different fields on different models describing the same data). I'm hoping someone can show me something cleaner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2024-10-12 01:44:02

这是一个(或多或少)整洁的解决方案,感谢 http://blog.abiss.gr/mgogoulos/entry/many_to_many_relationships_and< /a> 并修复了来自 http://code.djangoproject.com/ticket/5247 的 Django bug

from django.contrib import admin as admin_module

class SiteForm(ModelForm):
    user_profiles = forms.ModelMultipleChoiceField(
        label='Users granted access',
        queryset=UserProfile.objects.all(),
        required=False,
        help_text='Admin users (who can access everything) not listed separately',
        widget=admin_module.widgets.FilteredSelectMultiple('user profiles', False))

class SiteAdmin(admin_module.ModelAdmin):
    fields = ('user_profiles',)

    def save_model(self, request, obj, form, change):
        # save without m2m field (can't save them until obj has id)
        super(SiteAdmin, self).save_model(request, obj, form, change) 
        # if that worked, deal with m2m field
        obj.user_profiles.clear()
        for user_profile in form.cleaned_data['user_profiles']:
             obj.user_profiles.add(user_profile)

    def get_form(self, request, obj=None, **kwargs):
        if obj:
            self.form.base_fields['user_profiles'].initial = [ o.pk for o in obj.userprofile_set.all() ]
        else:
            self.form.base_fields['user_profiles'].initial = []
        return super(SiteAdmin, self).get_form(request, obj, **kwargs)

它使用与 filter_horizo​​ntal 设置相同的小部件,但硬编码到表单中。

Here's a (more or less) tidy solution, thanks to http://blog.abiss.gr/mgogoulos/entry/many_to_many_relationships_and and with a fix for a Django bug taken from http://code.djangoproject.com/ticket/5247

from django.contrib import admin as admin_module

class SiteForm(ModelForm):
    user_profiles = forms.ModelMultipleChoiceField(
        label='Users granted access',
        queryset=UserProfile.objects.all(),
        required=False,
        help_text='Admin users (who can access everything) not listed separately',
        widget=admin_module.widgets.FilteredSelectMultiple('user profiles', False))

class SiteAdmin(admin_module.ModelAdmin):
    fields = ('user_profiles',)

    def save_model(self, request, obj, form, change):
        # save without m2m field (can't save them until obj has id)
        super(SiteAdmin, self).save_model(request, obj, form, change) 
        # if that worked, deal with m2m field
        obj.user_profiles.clear()
        for user_profile in form.cleaned_data['user_profiles']:
             obj.user_profiles.add(user_profile)

    def get_form(self, request, obj=None, **kwargs):
        if obj:
            self.form.base_fields['user_profiles'].initial = [ o.pk for o in obj.userprofile_set.all() ]
        else:
            self.form.base_fields['user_profiles'].initial = []
        return super(SiteAdmin, self).get_form(request, obj, **kwargs)

This uses the same widget as the filter_horizontal setting, but hard-coded into the form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文