manytomany的管理模板

2024-04-27 19:16:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我在出版和病理学之间有很多关系。每一个出版物都可能有许多病态。当一个发布出现在管理模板中时,我需要能够看到与该发布相关联的许多病态。以下是示范声明:

class Pathology(models.Model):
    pathology = models.CharField(max_length=100)
    def __unicode__(self):
        return self.pathology
    class Meta:
        ordering = ["pathology"]

class Publication(models.Model):
    pubtitle = models.TextField()
    pathology = models.ManyToManyField(Pathology)
    def __unicode__(self):
        return self.pubtitle
    class Meta:
        ordering = ["pubtitle"]

这是管理员py. 我尝试过以下几种变体,但总是 出现一个错误,说明出版物或病理学没有外键 相关。在

^{pr2}$

谢谢你的帮助。在


Tags: selfmodelreturnmodelsdefunicode出版物meta
3条回答

这看起来更像是一对多的关系,虽然我有点不清楚确切的病理学是什么。另外,据我所知,Inlines在很多情况下不起作用。如果您颠倒模型的顺序,删除manytomy,并将ForeignKey字段添加到《病理学》中的Publication中,那么这种方法应该有效。在

class Publication(models.Model):
    pubtitle = models.TextField()
    def __unicode__(self):
        return self.pubtitle
    class Meta:
        ordering = ["pubtitle"]

class Pathology(models.Model):
    pathology = models.CharField(max_length=100)
    publication = models.ForeignKey(Publication)
    def __unicode__(self):
        return self.pathology
    class Meta:
        ordering = ["pathology"]

除非您使用的是本文所述的中间表http://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-intermediary-models,否则我不认为您需要创建内联类。尝试删除includes=[PathologyInline]行,看看会发生什么。在

我现在意识到Django对于网站的管理(数据输入)、简单的搜索和模板继承非常有用,但是Django和Python对于复杂的web应用程序不是很好,因为在数据库和html模板之间来回移动数据。我已经决定将Django和PHP结合起来,希望能利用两者的优点。谢谢你的帮助!在

相关问题 更多 >