python - Django/mysql中间表不插入数据

标签 python mysql django many-to-many

我在通知和用户之间有这 3 个表 M2M 关系,并且在该中间表中插入了历史表中的值。当我在历史表中生成内容时,中间表(notification_user)始终为空。 另一个问题,我想要在 notification_user 表中的值是历史表中的“状态”,而不是 id,对此有什么建议吗?

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

最佳答案

您无法插入数据,因为您的中间表包含超过 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)

关于python - Django/mysql中间表不插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672292/

相关文章:

python - 如何使用 pytorch 计算 softmax 回归的成本

python - 字典理解中的操作顺序

mysql - MySQL 中的 PREG_REPLACE 和 PREG_REPLACE_EVAL

mysql - 选择在另一个表中间接引用的行

MYSQL 使查询不区分大小写

django - 安装 django-CMS 后 .iPython 的权限被拒绝

python - 我想修饰 python 模块中的一个类。我该怎么做呢?

python - 给定列列表的 pandas 数据框的求和值

php - 如何在 PHP 和 MySQL 中使用 foreach 循环插入lastInsertId?

python - 如何在我的互联网服务器上运行 python (django) 应用程序?