Django / mysql中间表不插入数据

时间:2015-04-16 10:41:00

标签: python mysql django many-to-many

我在通知和用户之间有这3个表M2M关系,并且在该中间表中我已经从历史表中插入了一个值。当我在历史表中生成内容时,中间表(notification_user)总是空的。 另一个问题,我想在notification_user表中的值是" 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(我将数据插入历史表的方法)

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)

1 个答案:

答案 0 :(得分:1)

您无法插入数据,因为您的中间表包含两个以上的外键并且无法执行匹配。来自文档:

  

您的中间模型必须包含一个 - 并且只能包含一个 - 外键   到源模型(在我们的例子中这将是Group),或者你必须   显式指定Django应该使用的外键   使用ManyToManyField.through_fields的关系。如果你有更多   如果没有指定一个外键和through_fields,则进行验证   错误将被提出。类似的限制适用于外键   到目标模型(在我们的例子中这将是Person)。

如此处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)