Django表单 - 关联对象(模型表单集?)

时间:2012-11-20 23:05:23

标签: django django-views django-forms

说我有这样的事情:

class Product(models.Model)
    name = models.CharField()
    description models.TextField()

class Image(models.Model)
    product = models.ForeignKey(Product, null=True, blank=True, related_name="images")
    image = models.ImageField()

假设我在创建Product的表单中,并且在此表单中有一个部分允许您上传图像。这些图像是异步上传的。我怎么能这样:

创作时:
   产品已创建,图像与之相关。

编辑时:
   获取产品,获取相关图像。编辑产品,编辑图像。

目前,我的视图既可以创建也可以编辑Products。如何完成此产品表单中与图像相关的部分?模型表格集?

修改

@login_required
def create_or_edit_product(request, product_id=None):
    # Redirect user if he has no account associated to him
    try:
        account = Account.objects.get(membership__user=request.user)
    except:
        login_url = reverse('login') + ('?next=%s') % request.path
        return HttpResponseRedirect(login_url)

    # Get product if product id passed
    product_instance = None
    if product_id is not None:
        product_instance = get_object_or_404(product, id=product_id)

    # Get related pictures if product exists. Get picture values (dictionary list, used for initial formset data) if pictures found.
    pictures = product.pictures.all() if product_instance is not None else None
    pictures_values = pictures.values() if pictures else []

    PictureFormSet = formset_factory(PictureForm, formset=BasePictureFormSet, extra=0, can_delete=False, max_num=40)

    if request.method == "POST":
        product_form = productForm(request.POST, prefix='product', instance=product_instance)
        picture_formset = PictureFormSet(request.POST, prefix='pictures', initial=pictures_values)

        # If forms are valid
        if product_form.is_valid() and picture_formset.is_valid():
            try:
                # Add account to product and save
                product = product_form.save(commit=False)
                if product_instance is None:
                    product.account = account
                product.save()

                # Remove picture-product relationships of current pictures
                if pictures is not None:
                    pictures.update(product=None)

                # Update each picture with the product and corresponding sort order. (The field 'id' is a picture object. The form converts the passed picture id to a picture object)
                for index, form in enumerate(picture_formset):
                    picture = form.cleaned_data['id']
                    Picture.objects.filter(id=picture.id).update(product=product, sort_order=index)

            except Exception, e:
                # Rollback changes - something went wrong
                transaction.rollback()
                print "Transaction Rollback: %s" % e
            else:
                # Commit changes and redirect to product edit page
                transaction.commit()
                return HttpResponseRedirect('product_edit', product_id=product.id)
    else:
        product_form = productForm(prefix='product', instance=product_instance)
        picture_formset = PictureFormSet(prefix='pictures')

    # TODO: change add template to an add/edit one
    return render_to_response('products/add.html', {
        'product_form' : product_form,
        'picture_formset' : picture_formset,
        'pictures' : pictures,
    }, 
    context_instance=RequestContext(request))

我是Python和Django的新手,但这是我有点工作的观点。 (到目前为止我只尝试添加新产品)

用户看到的表单包含隐藏输入的缩略图,其中包含每个图片ID。在表单失败的情况下,我想重新显示缩略图和隐藏的输入(已经保留)。为了完成这项工作,我猜我必须查询图片ID,但表单无效,所以我该如何去做呢? (如果这是正确的道路)

您怎么看?

1 个答案:

答案 0 :(得分:0)

@Aamir Adnan是对的,Model Formsets是处理这个问题的优雅方式。

这是一个与您需要的完全相同的例子 - http://stellarchariot.com/blog/2011/02/dynamically-add-form-to-formset-using-javascript-and-django/

如示例所示,因为最终用户可能需要添加与产品相关的其他“图像”(在您的情况下),您将需要一些javascript逻辑来处理表单的动态添加,以便用户可以在保存时随意添加和上传图像。