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

Python 当替换一个对象时,模型中多个字段的顺序发生变化,python,django,Python,Django,我正试图为研究人员建立一个出版物数据库。所以我的数据库有模型类论文、作者和期刊。期刊是论文的外键,而作者是论文的多个键。因此,模型定义如下: 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.Char

我正试图为研究人员建立一个出版物数据库。所以我的数据库有模型类论文、作者和期刊。期刊是论文的外键,而作者是论文的多个键。因此,模型定义如下:

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())
所以基本上,被替换的作者对象会被另一个作者对象替换。由于这些都是论文中的作者,因此作者的顺序很重要。因此,我制作了一份作者(paper_authors)的副本,删除了paper_authors的所有项目,然后从副本中添加author对象,但需要替换的对象除外。但是作者的顺序被搞乱了。例如,假设有一篇论文的作者是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>]
B.BBB
B.BBB
[]
最初的
作者
[, ]
抄袭者
[, ]
后移
[]
[, ]
[]
[, ]
[, ]
[, ]
它在第二位插入了作者C.CCC。在我所忽略的许多领域中,是否存在一些排序方面的问题?我想维持订单


提前感谢。

感谢@Todor链接的可能副本。作为后续问题-如果我向序列中的另一个对象添加多个对象,QuerySet会生成另一个任意序列吗?当我第一次通过在序列中添加许多对象来生成数据库时,序列总是正确的。令人惊讶的是,仅当我尝试替换许多对象时,序列才被中断。您不能依赖此顺序(序列),因为您没有定义顺序,所以允许数据库以任何顺序返回结果。再次感谢。这就解决了我的问题。