更新单个Django模型字段

时间:2012-03-31 04:29:26

标签: python django

我有3个数据库模型 - Semester,Section和Notecard

Notecard模型有一个“已知”字段,我用它将Notecard对象分类为“已知(1)”或“未知(0)”的“桩”:

class Notecard(models.Model):
    notecard_name = models.CharField(max_length=50)
    notecard_body = models.TextField()
    section = models.ForeignKey(Section)
    known = models.BooleanField()

我有两个视图 - known_list和unkown_list,它们显示相应的堆(下面的known_list供参考):

def known_list(request, section_name):

    try:
        section = Section.objects.get(section_name__iexact = section_name)
    except Section.DoesNotExist:
        raise Http404

    known_list = Notecard.objects.filter(known=1, section=section)
    paginator = Paginator(known_list, 1)

    if known_list:    
        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1

        try:
            known = paginator.page(page)
        except (EmptyPage, InvalidPage):
            known = paginator.page(paginator.num_pages)

        context = RequestContext(request)
        return render_to_response('notecards/known.html', {"known": known}, context_instance=context)
    else:
        url = reverse('notecard_list', kwargs={'section_name': section_name})
        return HttpResponseRedirect(url)

此视图从上一个视图中引入section_name,以显示在单击的部分中以及已知堆中的所有Notecard对象。

在下面的模板中,您可以看到我将记事卡分页到一页:

{% extends "base.html" %}

{% block content %}
    <h1 class='title'><a href="/">NoteCards!</a></h1>
    {% for notecard in known.object_list %}
        <h1 class='notecard'>{{ notecard.notecard_name }}</h1>
        <h3 class='notecard'>{{ notecard.notecard_body }}</h3>
    {% endfor %}
    <div class="pagination">
    <span class="step-links">
        {% if known.has_previous %}
            <a class="navlink" href="?page={{ known.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ known.number }} of {{ known.paginator.num_pages }}
        </span>

        {% if known.has_next %}
            <a class="navlink" href="?page={{ known.next_page_number }}">next</a>
        {% endif %}
    </span>
    </div>
{% endblock %}

urls.py

urlpatterns += patterns('',
    url(r'^(?P<section_name>[\w|\W]+)/unknown/$', unknown_list, name="unknown_list"),
    url(r'^(?P<section_name>[\w|\W]+)/known/', known_list, name="known_list"),
    url(r'^semester/(?P<semester_name>[\w|\W]+)/', section_list, name="section_list"),
    url(r'^section/(?P<section_name>[\w|\W]+)/', notecard_list, name="notecard_list"),
    url(r'^notecard/(?P<notecard_name>[\w|\W]+)/', notecard_detail, name="notecard_detail"),
    url(r'^$', semester_list, name="semester_list"),
)

那就是说,我想添加一个“发送到未知”按钮,允许用户将他们当前所在页面的记录卡发送到未知堆(只需将已知字段更改为= 0,从中删除记录卡)分页列表,并移动到分页中的下一页。)

我试过复制我的new_notecard视图,其中包含完整形式的模型,但我无法弄清楚如何更新单个字段。

我也尝试过使用queryset.update()但是无法弄清楚如何从特定的记事卡中捕获pk。

我一直试图用自己来解决这个问题超过一个月,但我一直都没有成功。提前谢谢。

编辑:

似乎我挂断了在分页的每一页上拉着记录卡的pk。例如,如果我在分页的第3页 - 当按下“发送到未知”按钮时,如何在我的视图中识别该记录卡并将其从已知(1)更新为未知(0)

2 个答案:

答案 0 :(得分:2)

您必须使用特定网址创建特定视图才能处理此问题,例如:

# urls.py
url(r'^movetounknown/(?P<notecard_id>[\w|\W]+)/', notecard_move_to_unknown)

# views.py
@require_POST
def notecard_move_to_unknown(request, notecard_id):
    notecard = Notecard.objects.get(pk=notecard_id)
    notecard.known = False
    notecard.save()
    return HttpResponseRedirect(request.POST['next'])


# template
{% for notecard in known.object_list %}
    <h1 class='notecard'>{{ notecard.notecard_name }}</h1>
    <h3 class='notecard'>{{ notecard.notecard_body }}</h3>
    <form action="{% url views.move_to_unknown notecard.pk %}" method="post">
        <input type="hidden" name="next" value="{% url known_list known.section.section_name %}?page={{known.paginator.number}}"/>
        <input type="submit" value="Move to unknown list"/>
    </form>
{% endfor %}

您还可以将notecard ID作为post参数传递。 next参数告知更改后的位置,这里我选择已知列表的同一页面,因为一旦当前卡被删除,下一个就在此索引

答案 1 :(得分:0)

捕获特定notecard对象的pk可以通过为该记录卡定义特定URL来完成。例如: -

# urls.py
url(r'^notecard/(?P<notecard_id>\d+)/$',
    'notecard',
    name='notecard'),

# corresponding views.py
def notecard(request, note_card_id):
    notecard = get_object_or_404(Notecard, pk=note_card_id)
    template = 'notecard/notecard.html'
    template_vars = {'notecard': notecard}
    render(request, template, template_vars)

# notecard/notecard.html
<h2>{{ notecard.notecard_name }}</h2>
<p>{{ notecard.notecard_body }}</p>

您还可以定义一个表单,其中notecard id / pk是一个隐藏字段,用于提交和更新到您的数据库中(当然,您需要相应地更新您的视图功能)。

从本质上讲,要更新特定的notecard对象,您只需在您的视图功能中进行操作(如果您愿意,可以在列表页面中使用纯粹的ajax实现),就像这样

notecard.known = False
notecard.save()