许多关系与关系的附加数据有很多关系 [英] Many to many relationships with additional data on the relationship

查看:80
本文介绍了许多关系与关系的附加数据有很多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个记录我所有漫画购买的django数据库,我遇到了一些问题。我试图模仿一个漫画问题和与之相关的艺术家之间的关系。

I'm trying to create a django database that records all my comic purchases, and I've hit a few problems. I'm trying to model the relationship between a comic issue and the artists that work on it.

漫画问题有一个或多个艺术家在这个问题上工作,一个艺术家将致力于一个以上的问题。此外,艺术家还有他们在漫画创作者(所有关于这个问题的工作),作家(写剧本),抽屉(绘制完整的漫画),铅笔,油墨,颜色或文字上所做的工作,并且可能有几个艺术家在给定的角色。

A comic issue has one or more artists working on the issue, and an artist will work on more than a single issue. In addition, the artist has a role relating to what they did on the comic – creator (all the work on that issue), writer (wrote the script), drawer (drew the complete comic), pencils, inks, colours or text, and there may be several artists in a given role.

这给我一个数据库模型,如:

This gives me a database model like:

然后我将其转换为以下Django模型。
由于我需要关于关系的其他数据,我相信我必须使用一个单独的类来处理关系,并持有额外的

I then translate this into the following Django model. As I require additional data on the relationship, I believe I have to use a separate class to handle the relationship, and hold the additional

class Artist(models.Model):
    name = models.CharField(max_length = 100)

    def __unicode__(self):
        return self.name

class ComicIssue(models.Model):
    issue_number = models.IntegerField()
    title = models.TextField()
    artists = models.ManyToManyField(Artist, through='IssueArtist')

    def __unicode__(self):
        return u'issue = %s, %s' % (self.issue_number, self.title)

class IssueArtist(models.Model):
    roles = ( (0, "--------"),
              (1, "Creator"),
              (2, "Writer"),
              (3, "Drawer"),
              (4, "Pencils"),
              (5, "Inks"),
              (6, "Colours"),
              (7, "Text"),
            )
    artist = models.ForeignKey(Artist)
    issue = models.ForeignKey(ComicIssue)
    role = models.IntegerField(choices = roles)

我的问题是:

1)这似乎是一个正确的建模方式吗?

1) Does this seem a correct way of modelling this?

2)如果我不使用 through ='IssueArtist'功能,我可以使用 artists.add添加关系()函数。如果我使用这个,我会收到一个错误'ManyRelatedManager'对象没有属性'add'。我必须通过创建 IssueArtist()实例来手动管理关系,并明确地搜索关系表?
NB。我正在使用Django 1.0,目前

2) If I don't use the through='IssueArtist' feature, I can add relationships by using the artists.add() function. If I do use this, I get an error 'ManyRelatedManager' object has no attribute 'add'. Do I have to manually manage the relationship by creating IssueArtist() instances, and explicitly searching the relationship table? NB. I am using Django 1.0, at the moment

推荐答案

我建议您查看多个地方的多个地方关系在 Django文档 - 它涵盖了你的问题。

I recommend you check out the section "EXTRA FIELDS ON MANY-TO-MANY RELATIONSHIPS" in the Django documentation - it covers exactly your question.

1:是的,创建中介模型是要走的路。

1: yes, creating an intermediary model is the way to go.

2:从文档中: 与正常的多对多领域不同,您不能使用添加,创建或分配来创建关系 - 而是将艺术家添加到问题中,从中介模型中执行:

2: From the documentation: "Unlike normal many-to-many fields, you can't use add, create, or assignment to create relationships" - Instead, for adding Artist to an Issue, do it from the intermediary model:

some_artist = Artist.objects.get(name='some name')
some_issue = Issue.objects.get(tile='some tite')
IssueArtist.objects.create(artist= some_artist, issue = some_issue, role=1)

这篇关于许多关系与关系的附加数据有很多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆