【问题标题】:Using Django's "update_or_create" function, returns nothing for me使用 Django 的“update_or_create”函数,对我没有任何回报
【发布时间】:2016-07-28 17:21:39
【问题描述】:

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

首先,我使用 Django RestFramework 从我的 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

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

       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

这是我的模板:

 {% for form in formset %} 
      <div>first_name: {{ form.name.value }}</div>          
 {% endfor %}

但没有输出。空。

我是否错误地使用了 update_or_create?还有什么?

【问题讨论】:

    标签: django django-forms modelform formset


    【解决方案1】:

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

    【讨论】:

    • 我想通了!我的数据库都搞砸了,所以清理它并再次运行!感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    相关资源
    最近更新 更多