[Answer]-Django many-to-many model simple logic

1👍

You should use ManyToManyField instead. It creates the intermediary join table for you and manages it.

Examples from Django docs:

from django.db import models

class Publication(models.Model):
    title = models.CharField(max_length=30)

    def __unicode__(self):
        return self.title

    class Meta:
        ordering = ('title',)

class Article(models.Model):
    headline = models.CharField(max_length=100)
    publications = models.ManyToManyField(Publication)

    def __unicode__(self):
        return self.headline

    class Meta:
        ordering = ('headline',)

Example:
https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

Field docs:
https://docs.djangoproject.com/en/dev/ref/models/fields/#ref-manytomany

Leave a comment