[Answer]-Django/mysql intermidiate table not inserting data

1๐Ÿ‘

โœ…

You cannot insert data because your intermediate table contains more than 2 foreign keys and cannot perform the matching. From documentation:

Your intermediate model must contain one โ€“ and only one โ€“ foreign key
to the source model (this would be Group in our example), or you must
explicitly specify the foreign keys Django should use for the
relationship using ManyToManyField.through_fields. If you have more
than one foreign key and through_fields is not specified, a validation
error will be raised. A similar restriction applies to the foreign key
to the target model (this would be Person in our example).

As stated here https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ManyToManyField.through_fields, you can use through_fields. The example matches your case:

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=50)

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership', through_fields=('group', 'person'))

class Membership(models.Model):
    group = models.ForeignKey(Group)
    person = models.ForeignKey(Person)
    inviter = models.ForeignKey(Person, related_name="membership_invites")
    invite_reason = models.CharField(max_length=64)

Updated answer:

Also, to create m2m relationship in your view.py:

Notification_User.objects.create(user = UserObject, notification = NotificationObject)

Leave a comment