合并多对多关系中的模型对象

时间:2016-05-13 12:48:44

标签: python django django-orm

我正在尝试将我的Django应用程序中的某些对象合并在一起。我从Wordpress网站痛苦地迁移了数据。该网站是针对警察事件的存档我使用两种模式(事件,官员)与多种多对多关系(详情)作为中间模型。这是我的models.py:

from __future__ import unicode_literals
from django.db import models

class Officer(models.Model):
    first_name = models.CharField(max_length=80, blank=True)
    last_name = models.CharField(max_length=80, blank=True)
    badge = models.IntegerField(blank=True)
    department = models.CharField(max_length=50, blank=True)

    def __unicode__(self):
        return self.last_name + ', ' + self.first_name

class Incident(models.Model):
    officers2 = models.ManyToManyField(Officer, through='Details')
    case_number = models.CharField(max_length=50, blank=True)

    OFFICE_CHOICES = (
    ('CRA', 'Civilian Review Authority'),
    ('IA', 'Internal Affairs'),
    ('OPCR', 'Office of Police Conduct Review'),
    )
    office = models.CharField(max_length=10,
    choices=OFFICE_CHOICES,
    )

    def __unicode__(self):
        return self.case_number

class Details(models.Model):
    officer = models.ForeignKey(Officer, on_delete=models.CASCADE)
    incident = models.ForeignKey(Incident, on_delete=models.CASCADE)
    allegation = models.CharField(max_length=50, blank=True)
    finding = models.CharField(max_length=50, blank=True)
    action = models.CharField(max_length=50, blank=True)


    def __unicode__(self):
        return unicode(self.officer.first_name) + ' '+ unicode(self.officer.last_name)+ ', ' + unicode(self.incident)

    class Meta():
        verbose_name_plural = "details"

问题是目前有重复的事件具有相同的案件编号,涉及不同的人员。我正在尝试将这些内容合并为一个事件,同时保留详细信息中的所有信息。这是我尝试过的脚本,它不太合适。

from __future__ import unicode_literals
from police_archive.models import Incident, Details, Officer
from django.db.models import Count



def run() :

    #get list of duplicates
    list=Incident.objects.values('case_number').annotate(case_number_count=Count('case_number')).exclude(case_number_count=1)
    g= []

    #make list of case numbers
    for item in list:
        g.append(item['case_number'])



    #first entry was empty, remove first entry
    for number in g[1:] :

        #get list of incident objets
        old_incidents=Incident.objects.filter(case_number=number);

        #make new object to replace old ones
        new_incident = Incident(case_number=number)
        new_incident.office=old_incidents[0].office

        #filter out all details which match one of the incidents
        details_list=Details.objects.none()
        for incident in old_incidents:
            print incident.officer2
            results=Details.objects.filter(incident=incident)
            details_list = details_list | results

        #delete old_incidents so that details can be modified
        old_incidents.delete()

        #create new details objects pointing to new incident object, delete old details object
        for details in details_list:
            new_details = Details(incident=new_incident,officer=details.officer, allegation=details.allegation, finding=details.finding, action=details.action)
            new_details.save()
            details.delete()
            #details.save()
        new_incident.save()

此脚本的结果是正确创建事件对象,但不会创建详细信息对象。我已经在shell中尝试过该脚本的微版本,并且效果很好。我目前没有想法;如果你有,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:0)

在保存详细信息之前,您需要保存new_incident。您将在新事件进入数据库之前保存新详细信息。