views:

32

answers:

2

I have this simple model:

class Tag(models.Model):
    title = models.SlugField()
    created = models.datetime
    def __unicode__(self):
        return self.title

class Entry(models.Model):
    title = models.CharField(max_length=30)
    created = models.datetime
    tags = models.ForeignKey(Tag)
    categories = models.CharField(max_length=15)
    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name_plural = "Entries"

I need to be able to attach multiple tags to the entry so that it could be saved to the database. How can I do that? Now there is only one tag assigned.

+3  A: 

As one tag can have many entries and vice versa, you will want to add a ManyToMany field.

biocs
I have changed tags = models.ForeignKey(Tag)to tags = models.ManyToManyField(Tag)Then I have updated the database to be consistent with this model and it worked just fine! Thanks!
Sergey Basharov
A: 

I would create another class in the Model to support this.

class tagEntryJoins(models.Model): 
     tag = models.ForeignKey('Tag')
     entry = models.ForeignKey('entry') 
Dean