Reputation: 163
I have a database in mysql and I am setting up a django project. I have some entities with many-to-many relations, which are handled through association tables. With django, I have understood that I can use ManyToMany entity to achieve many-to-many relations BUT how do I do when the association table holds more information besides just the many-to-many relation? See the example below.
CREATE TABLE `products` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` char(150) DEFAULT NULL ...)
CREATE TABLE `pictures` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`filename` char(100) NOT NULL ... )
CREATE TABLE `products_pictures` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`fk_products_id` int(11) unsigned NOT NULL,
`fk_pictures_id` int(11) unsigned NOT NULL,
`priority` tinyint(4) unsigned DEFAULT NULL,
`relation` varchar(25) DEFAULT 'same product family' ... )
Upvotes: 1
Views: 93
Reputation:
you can use through option, for example
class Products(models.Model):
name = models.CharField(max_length=128)
pictures = models.ManyToManyField(
Pictures,
through='ProductsPictures',
)
class ProductsPictures(models.Model):
product = models.ForeignKey(Products, on_delete=models.CASCADE)
picture = models.ForeignKey(Pictures, on_delete=models.CASCADE)
description = models.CharField(max_length=128)
Upvotes: 4