English 中文(简体)
Many to many relationships with additional data on the relationship
原标题:

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: Database model

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)

My questions are:

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

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

最佳回答

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

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

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)
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签