使用Django" update_or_create"功能,对我没有任何回报

时间:2016-04-07 17:02:48

标签: django django-forms modelform formset

所以,我正在使用Django Model Formsets。

首先,我使用Django RestFramework从我的API中获取内容:

function say(word) {
    this.saying = word;
    this.container = document.getElementById('result1');
    return handleEvent.bind(this);
}

function shout(word) {
    this.saying = word.toUpperCase();
    this.container = document.getElementById('result2');
    return handleEvent.bind(this);
}

function handleEvent() {
    var text = document.createTextNode(this.saying);
    this.container.innerHTML = '';
    this.container.appendChild(text);
}

然后,我尝试用我的API中的数据填充我的本地数据库,如下所示:

    itemsQuery = People.objects.using('api').prefetch_related('staff').filter(id__in=['293992'])

    ### I can dump itemsQuery and get a bunch of data that I need
    ### Ie: logger.error(itemsQuery.values()), so I know stuff is in there

这是我的模板:

       for people in itemsQuery:
        allPeople = NewPeople.objects.update_or_create(
             name=people.staff.name,
        )

        ### I can dump the data out like so:
        ### logger.error(people.staff.name)
        ### and get a bunch of names, so I know that part is at least working
        ### and "name" is a field in my model

        something = NewPeople.objects.filter(section_id__in=['293992'])

       formset = PeopleFormSet(queryset=something)

       context['formset'] = formset

       return context

但没有输出。空。

我是否错误地使用了update_or_create?还有别的吗?

1 个答案:

答案 0 :(得分:1)

您已使用name创建了NewPeople对象;你还没有设置任何其他属性。但是当您获得something查询集时,请按section_id进行查询。你永远不会设置任何部分ID;这样查询将始终为空。