Django - 具有一对多关系的模型的更新页面

时间:2012-06-18 01:53:21

标签: django django-forms django-views

我有一个Django应用程序,我有Post和PostImage模型。

每个帖子都有多个PostImages。

在Post添加页面中,我有Post和PostImages的表单。我已经为PostImages使用了formset。这是视图的代码:

def post_new(request, template_name="posts/new.html"):


  post_form = Post(request.POST or None, request.FILES or None)
  PostImageFormSet = formset_factory(PostImageForm, extra=1)
  post_image_formset = PostImageFormSet(request.POST or None, request.FILES or None, prefix='post_images')

  if post_form.is_valid() and post_image_formset.is_valid():
    post = post_form.save(commit=False)
    post.save()
    for post_image_form in post_image_formset:
      post_image = post_image_form.save(commit=False)
      post_image.post = post
      post_image.save()

    redirect_url = urlresolvers.reverse('post_show')
    return HttpResponseRedirect(redirect_url)

  form_data = {'post_form': post_form, 'post_image_formset': post_image_formset}
  csrfContext = RequestContext(request, form_data)
  return render_to_response(template_name, csrfContext)

我使用jQuery为PostImage添加更多表单,它工作得很好,我能够创建一个包含我想要的图像的帖子。

现在,我希望我的post_edit页面以类似的方式工作。这是我的post_edit视图方法的代码:

def post_edit(request, post_slug, template_name="developers/posts/edit.html"):
  post = get_object_or_404(post, slug=post_slug)
  post_images = PostImage.objects.filter(post_id=post.id)
  count = len(post_images)
  post_form = postForm(request.POST or None, request.FILES or None, instance=post)
  postImageFormSet = formset_factory(postImageForm, extra=count)
  post_image_formset = postImageFormSet(request.POST or None, request.FILES or None, prefix='post_images')

  for post_image_form, post_image in zip(post_image_formset.forms, post_images):
    post_image_form.initial = post_image
  if post_form.is_valid():
    post_form.save()
    redirect_url = urlresolvers.reverse('developer_post_show', args=[post_slug])
    return redirect(redirect_url)

  form_data = {'post_form': post_form, 'post_image_formset': post_image_formset}
  csrfContext = RequestContext(request, form_data)
  return render_to_response(template_name, csrfContext)

这是我的编辑页面模板:

{% extends "base.html" %}

{% block content %}
{% load thumbnail %}

  {% if error_message %} <p><strong>{{ error_message }}</strong></p> {% endif %}
  {{ post_form.errors }}
  <div style="width: 80%; margin: 0 auto;">
    <form action="" method="post">
      {% csrf_token %}
      <div>
        ... Post related HTML
      </div>

      <div class="post-images" style="clear: both;">
        {% for post_image_form in post_image_formset %}
          <div>
            {{ post_image_form.caption  }}
          </div>
        {% endfor %}
      </div>

      <div style="clear: both; position: relative; left: 320px;">
        <input type="submit" value = "Update post" />
      </div>

    </form>
  </div>
{% endblock %}

我的模板中出现错误:

'PostImage'对象没有属性'get'

在我的模板中,我遍历 post_image_formset 对象,并且它迭代3次,这是正确的,因为该特定帖子有3个图像。但我仍然得到这个错误。有人可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:0)

您的PostImageFormModelForm。在编辑视图中,使用initial属性提供现有实例。这不是推荐的方式,您的错误可能是这种情况的症状。

To provide initial instances a QuerySet is used.

在你的情况下,这将是这样的:

def post_edit(request, post_slug, template_name="developers/posts/edit.html"):
    post = get_object_or_404(post, slug=post_slug)
    post_images = PostImage.objects.filter(post_id=post.id)

    PostImageFormSet = formset_factory(postImageForm, extra=2)
    formset = PostImageFormSet(queryset=post_images)

您可能还想使用推荐的处理方式POST vs. GET requests in form views