Django/mysql 中间 table 不插入数据

Django/mysql intermidiate table not inserting data

我在通知和用户之间有这 3 tables M2M 关系,并且在那个中间 table 我从历史 table 中插入了一个值。当我在历史 table 中生成内容时,中间 table (notification_user) 总是空的。 另一个问题,我在 notification_user table 中想要的值是历史 table 中的 "status",而不是 id,对此有什么建议吗?

Models.py

class Notification(models.Model):
    title = models.CharField(max_length=75)
    description = models.TextField()
    start_date = models.DateTimeField()
    end_date = models.DateTimeField()
    application = models.ManyToManyField('Products.Application')
    notificationType = models.ForeignKey(NotificationType)
    url = models.URLField(null=True, blank=True)
    country = models.ManyToManyField('Geolocations.Country', null=True, blank=True)
    browser = models.CharField(max_length=35, null=True, blank=True)
    image = models.ImageField(null=True, blank=True)
    ip = models.IPAddressField(null=True, blank=True)
    user = models.ManyToManyField(User, through='Notification_User')

class History(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    create_at = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=20)
    ip = models.IPAddressField()
    country = models.CharField(max_length=30, null=True, blank=True)
    city = models.CharField(max_length=30, null=True, blank=True)
    browser = models.CharField(max_length=30, null=True, blank=True)
    os = models.CharField(max_length=30, null=True, blank=True)
    notification = models.ForeignKey(Notification)

class Notification_User(models.Model):
    user = models.ForeignKey(User)
    notification = models.ForeignKey(Notification)
    status = models.ForeignKey(History, null=True, blank=True)

views.py(我将数据插入历史的方法table)

notifications = Notification.objects.filter(**condition).\
    exclude(history__user_id__userid=userid, history__status=1).\
    order_by('notificationType__priority')

for n in notifications:
    nid = n.id
    user = User(userid=userid, username=username)
    user.save()
    history = {'user': user,
               'status': " ",
               'ip': ip,
               'country': country,
               'city': city,
               'browser': k[1],
               'os': k[0],
               'notification_id': nid
    }
    History.objects.create(**history)

您无法插入数据,因为您的中间 table 包含超过 2 个外键,无法执行匹配。来自文档:

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).

如此处所述https://docs.djangoproject.com/en/1.7/ref/models/fields/#django.db.models.ManyToManyField.through_fields,您可以使用through_fields。该示例符合您的情况:

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)

更新的答案:

此外,要在 view.py 中创建 m2m 关系:

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