1

我有一组已知的 JSON 数据,从我想添加到我的 Django 应用程序的 Excel 文件中提取。格式如下所示:

[{" Record": 12345,
"Event":" Initial task completed",
"TeamID": 12345,
"IndiviualID":null,
"Description":" Just a description",
"Date": "1/3/13 9:00"},{" Record": 5555,
"Event":" A different task completed",
"TeamID": 9999,
"IndiviualID":null,
"Description":" Just another description",
"Date": "1/13/13 6:00"}]

假设我有一个名为 Member 的模型。如何从此 JSON 数据创建成员,而不必通过我的表单手动提交?希望这是有道理的。谢谢。

附录:我还必须澄清一下,我在 Django 应用程序上的对象有一些额外的变量,并丢弃了其他变量,所以它不是完全匹配的。什么算法会起作用?

更新:( 与电子表格数据不同,当前的实现是数据的 2 模型版本。也就是说,每个成员都有一个名为 Data 的相关对象,它携带大部分变量。记录和日期是实际在成员对象中的唯一变量.所有其他都是Data对象的一部分)

这是我的模型安排。

class Member(models.Model):
    def __unicode__(self):
        return self.record

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
    was_published_recently.admin_order_field = 'pub_date'
    was_published_recently.boolean = True
    was_published_recently.short_description = 'Entered recently?'

    record = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date')


class Data(models.Model):
    def __unicode__(self):
        return self.dob

    member = models.ForeignKey(Member)  
    dob = models.CharField(max_length=200)
    event = models.CharField(max_length=200)
    description = models.CharField(max_length=200)
    gender = models.CharField(max_length=200)
4

1 回答 1

3

好的,这可行,但是我不得不对您的模型进行一些更改,所以也许我不明白您想要做什么。

带有我也添加到 json 中的名称字段的模型,因此可以放入一些数据,并且所有字段都可以留空以容纳不完整的数据。

class Member(models.Model):

    name = models.CharField(max_length=30)
    record = models.CharField(max_length=200, blank=True, null=True)
    pub_date = models.DateTimeField('date', blank=True, null=True)


class Data(models.Model):

    member = models.ForeignKey(Member)
    dob = models.CharField(max_length=200, blank=True, null=True)
    event = models.CharField(max_length=200, blank=True, null=True)
    description = models.CharField(max_length=200, blank=True, null=True)
    gender = models.CharField(max_length=200, blank=True, null=True)

    def save(self, *args, **kwargs):

        member, _ = Member.objects.get_or_create(name = self.name)
        # can update member here with other fields that relate to them
        self.member = member
        super(Data, self).save(*args, **kwargs)

下面的代码将所有 json 值放入 Data 的实例中,然后在 Data 的 save 方法中,创建一个新的 Member 实例。未使用的值被简单地丢弃。

    json = [{" Record": 12345,
"Name": "Joe",
"Event":" Initial task completed",
"TeamID": 12345,
"IndiviualID":"",
"Description":" Just a description",
"Date": "1/3/13 9:00"},{" Record": 5555,
"Name": "Jane",
"Event":" A different task completed",
"TeamID": 9999,
"IndiviualID":"",
"Description":" Just another description",
"Date": "1/13/13 6:00"}]

    for item in json:

        d = Data()

        for k,v in item.iteritems():
            setattr(d, k.lower(), v)

        d.save()

结果是:

成员

1   Joe     
2   Jane        

数据

1   1        Initial task completed  Just a description 
2   2        A different task completed  Just another description   
于 2013-09-10T18:10:26.977 回答