替换一个对象时,模型中ManytoMany字段的顺序发生了变化

时间:2017-03-11 22:48:51

标签: python django

我正在尝试为研究人员建立一个出版物数据库。所以我的数据库有模型类Paper,Author和Journal。期刊是Paper的外键,而Author是ManytoMany Paper的关键。因此,模型定义是:

from django.db import models
from django import forms
from django.forms import ModelForm, Textarea

# Create your models here.

class Journal(models.Model):
    name = models.CharField(max_length = 100)
    organization = models.CharField(max_length = 100, blank = True)

    def __unicode__(self):
        return self.name


class Author(models.Model):
    first_name = models.CharField(max_length = 20, blank = True)
    last_name = models.CharField(max_length = 20, blank = True)
    middle_name = models.CharField(max_length = 20, blank = True)
    full_name = models.CharField(max_length = 50)
    email = models.EmailField(blank = True)

    def __unicode__(self):
        return self.full_name


class Paper(models.Model):
    paper_title = models.CharField(max_length=200)
    paper_year = models.IntegerField(blank = True, null = True)
    paper_volume = models.CharField(max_length = 100, blank = True, null = True)
    paper_number = models.CharField(max_length = 100, blank = True, null = True)
    paper_pages = models.CharField(max_length = 100, blank = True, null = True)
    paper_month = models.CharField(max_length = 15, blank = True, null = True)
    paper_doi = models.CharField(max_length = 50, blank = True, null = True)
    paper_abstract = models.TextField(blank = True, null = True)
    paper_keywords = models.TextField(blank = True, null = True)
    paper_journal = models.ForeignKey(Journal)
    paper_authors = models.ManyToManyField(Author)

    def __unicode__(self):
        return self.paper_title

我创建了一个数据库,现在我正在尝试使用表单让用户编辑论文。我正在使用ModelForm来创建表单。要编辑论文的作者信息,我使用下拉列表让用户知道论文中的特定作者可能是重复的。然后,用户可以检出另一个作者,如果确实是重复的作者条目,则可以将另一个作者的数据插入到本文中。用于此目的的代码块是:

other_author = Author.objects.get(id = other_author_srno)
print(other_author)
replaced_author = Author.objects.get(id = author_srno)
print(replaced_author)
replaced_author_papers = replaced_author.paper_set.all()
print(replaced_author_papers)
for paper_item in replaced_author_papers:
    print("initial")
    print("authors")
    paper_item_authors = paper_item.paper_authors.all()
    print(paper_item_authors)
    copy_of_paper_item_authors = []
    for author in paper_item_authors:
        copy_of_paper_item_authors.append(author)
    print("copyofauthors")
    print(copy_of_paper_item_authors)
    for author in paper_item_authors:
        paper_item.paper_authors.remove(author)
    print("afterremove")
    print(paper_item.paper_authors.all())
    print(copy_of_paper_item_authors)
    for author in copy_of_paper_item_authors:
        if not author == replaced_author:
            paper_item.paper_authors.add(author)
        else:
            paper_item.paper_authors.add(other_author)
        paper_item.save()
        print(paper_item.paper_authors.all())
    print(paper_item.paper_authors.all())

所以基本上replacement_author对象被other_author对象替换。由于这些是论文中的作者,作者的顺序很重要。所以我复制了作者(paper_authors),删除了paper_authors的所有项目,然后从副本中添加了作者对象,除了需要替换的对象。但作者的顺序搞砸了。例如,假设有一篇论文与作者A. AAA,B。BBB,C。CCC,还有另外一篇论文与这些相同的作者。我想逐个替换它们,因为它们是相同的条目。但如果我尝试替换作者B. BBB,这就是输出。

B. BBB
B. BBB
[<Paper: XYZ>]
initial
authors
[<Author: A. AAA>, <Author: B. BBB>, <Author: C. CCC>]
copyofauthors
[<Author: A. AAA>, <Author: B. BBB>, <Author: C. CCC>]
afterremove
[]
[<Author: A. AAA>, <Author: B. BBB>, <Author: C. CCC>]
[<Author: A. AAA>]
[<Author: A. AAA>, <Author: B. BBB>]
[<Author: A. AAA>, <Author: C. CCC>, <Author: B. BBB>]
[<Author: A. AAA>, <Author: C. CCC>, <Author: B. BBB>]

它将作者C. CCC插入第二位。我忽略了ManyToMany字段中的某些排序方面吗?我希望保持订购。

提前致谢。

0 个答案:

没有答案