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.

我希望能够使用任何多选接口编辑M2M关系的两面(最好是在管理页面上,尽管需要它可以在普通页面上)。

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.

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

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

DavidM

1 个解决方案

#1


2

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

没有任何事情自动发生的原因是“项目”字段不是Tag模型的一部分。这意味着你必须自己完成所有工作。像(在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.

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

更多相关文章

  1. gsutil - 正则表达式与替代不工作
  2. 【小白自学笔记】【机器学习实战】【Python代码逐行理解】CH02
  3. 三个猜数字游戏代码(Python)
  4. 在混合的Bash-Python代码片段中,变量的双引号和单引号
  5. 八大经典排序算法基本思想及代码实现(插入排序,希尔排序,选择排序,
  6. 贝叶斯学习 -- matlab、python代码分析(3)
  7. UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的sh
  8. Linux下objdump查看C程序编译后的汇编代码
  9. DDNS 的工作原理及其在 Linux 上的实现--转

随机推荐

  1. golang中sort包如何实现
  2. emacs支持golang吗
  3. golang用户登录怎么做
  4. dart和golang区别
  5. go语言中包导入的一些问题
  6. golang判断是否存在不存在就创建文件
  7. golang的堆栈怎么看
  8. golang中定义不定长数组的方法
  9. golang不适合开发web吗
  10. golang中的map是结构体吗