1

说我有这样的事情:

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,但表格无效,那么我该怎么做呢?(如果这甚至是正确的道路)

你怎么看?

4

1 回答 1

0

@Aamir Adnan 是对的,这Model Formsets是一种优雅的处理方式。

这是一个与您需要的类似的完整示例 - http://stellarchariot.com/blog/2011/02/dynamically-add-form-to-formset-using-javascript-and-django/

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

于 2012-11-21T00:22:41.917 回答