我正在写一个小网站来存储我写的论文。关系论文<->作者很重要,但作者姓名的顺序(哪个是第一作者,哪个是第二作者,等等)也很重要。
我只是在学习 Django,所以我知道的不多。无论如何,到目前为止我已经完成了:
from django.db import models
class author(models.Model):
Name = models.CharField(max_length=60)
URLField = models.URLField(verify_exists=True, null=True, blank=True)
def __unicode__(self):
return self.Name
class topic(models.Model):
TopicName = models.CharField(max_length=60)
def __unicode__(self):
return self.TopicName
class publication(models.Model):
Title = models.CharField(max_length=100)
Authors = models.ManyToManyField(author, null=True, blank=True)
Content = models.TextField()
Notes = models.TextField(blank=True)
Abstract = models.TextField(blank=True)
pub_date = models.DateField('date published')
TimeInsertion = models.DateTimeField(auto_now=True)
URLField = models.URLField(verify_exists=True,null=True, blank=True)
Topic = models.ManyToManyField(topic, null=True, blank=True)
def __unicode__(self):
return self.Title
在我现在可以定义作者是谁的意义上,这项工作很好。但我不能订购它们。我该怎么做?
当然我可以添加一系列关系:第一作者,第二作者,...
但它会很丑,而且不灵活。有什么更好的主意吗?
谢谢
最佳答案
您可以添加 'through' model到那个 ManyToMany 关系并在该模型中,存储一个值,该值显示作者在与出版物的特定关联中应该出现的顺序。
(额外提示 - 它可能会帮助您深入了解 - 就清晰度而言 - 对类/模型名称使用首字母大写,并为属性和实例/对象使用小写字母 - 这样,如果您与他人共享代码,它”会更容易,因为它是一种常见的模式)
关于django - 在 Django 模型中按多对多关系排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2893080/