在管理页面中编辑M2M的两面 [英] Editing both sides of M2M in Admin Page

查看:111
本文介绍了在管理页面中编辑M2M的两面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将展示我想要实现的情况,以防止有其他方法!



我想要能够编辑两者M2M关系的一面(最好在管理页面上,但是如果需要,可以在正常页面上)使用任何多选择界面。



问题显然是反面,作为主要方面(关系定义)在自动运行中起作用。



我已经尝试了一些建议,以获得在线显示而且这个工作不是很好的界面。



我在django邮件列表中提供的建议是使用一个自定义的ModelForm。我已经得到一个多重选择框出现,但它似乎没有被连接到任何东西,因为它不是从所选的任何东西开始,并且不保存所做的任何更改。



这是适当的代码片段:

 #models.py 
class Tag(models .Model):
name = models.CharField(max_length = 200)

class Project(models.Model):
name = models.CharField(max_length = 200)
description = models.TextField()
tags = models.ManyToManyField(Tag,related_name ='projects')

#admin.py
class TagForm(ModelForm):
fields =('name','projects')
projects = ModelMultipleChoiceField(Project.objects.all(),widget = SelectMultiple())
class Meta:
model =

class TagAdmin(admin.ModelAdmin):
fields =('name','projects')
form = TagForm
/ pre>

任何帮助将不胜感激,要么获得上面的代码,要么提供更好的方法来实现!



DavidM

解决方案

自动发生的原因是项目字段不是标签模型的一部分。这意味着你必须自己做所有的工作。某些东西(在TagForm中):

  def __init __(self,* args,** kwargs):
super TagForm,self).__ init __(* args,** kwargs)
if'instance'in kwargs:
self.fields ['projects']。initial = self.instance.project_set.all()

def save(self,* args,** kwargs):
super(TagForm,self).save(* args,** kwargs)
self.instance.project_set。 clear()
for self.cleaned_data ['projects']中的项目:
self.instance.project_set.add(project)

请注意,代码未经测试,因此您可能需要调整它才能使其正常工作。


First I'll lay out what I'm trying to achieve in case there's a different way to go about it!

I want to be able to edit both sides of an M2M relationship (preferably on the admin page although if needs be it could be on a normal page) using any of the multi select interfaces.

The problem obviously comes with the reverse side, as the main side (where the relationship is defined) works just fine automagically.

I have tried some of the advice here to get an inline to appear and that works but its not a very nice interface.

The advice I got on the django mailing list was to use a custom ModelForm. I've got as far as getting a multiselect box to appear but it doesnt seem to be "connected" to anything as it does not start with anything selected and does not save any changes that are made.

Here's the appropriate snippets of code:

#models.py
class Tag(models.Model):
    name = models.CharField(max_length=200)

class Project(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField()
    tags = models.ManyToManyField(Tag, related_name='projects')

#admin.py
class TagForm(ModelForm):
    fields = ('name', 'projects')
    projects = ModelMultipleChoiceField(Project.objects.all(), widget=SelectMultiple())
    class Meta:
        model = Tag

class TagAdmin(admin.ModelAdmin):
    fields = ('name', 'projects')
    form = TagForm

Any help would be much appreciated, either getting the code above to work or by providing a better way to do it!

DavidM

解决方案

The reason why nothing happens automatically is that the "projects" field is not a part of the Tag model. Which means you have to do all the work yourself. Something like (in TagForm):

def __init__(self, *args, **kwargs):
    super(TagForm, self).__init__(*args, **kwargs)
    if 'instance' in kwargs:
        self.fields['projects'].initial = self.instance.project_set.all()

def save(self, *args, **kwargs):
    super(TagForm, self).save(*args, **kwargs)
    self.instance.project_set.clear()
    for project in self.cleaned_data['projects']:
        self.instance.project_set.add(project)

Note that the code is untested so you might need to tweek it some to get it to work.

这篇关于在管理页面中编辑M2M的两面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆