Django表单-相关对象(模型表单集?)
假设我有这样的东西:Django表单-相关对象(模型表单集?),django,django-views,django-forms,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()
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()
假设我在一个用于创建产品的表单中,在这个表单中有一个部分允许您上传图像。这些图像是异步上传的。我怎样才能做到:
关于创建:
产品被创建,图像与之相关
关于编辑:
获取产品,获取相关图像。编辑产品,编辑图像
目前,我有一个视图,既可以创建产品,也可以编辑产品。如何完成此产品表单中与图像相关的部分?模型模板集
编辑:
我不熟悉Python和Django,但这是我的工作观点。到目前为止,我只尝试添加一个新产品
用户看到的表单具有包含每个图片id的隐藏输入的缩略图。如果表单失败,我希望重新显示缩略图以及已保留的隐藏输入。要做到这一点,我想我必须查询图片ID,但是表单无效,那么我该如何做呢?如果这是一条正确的道路
你认为呢?@Aamir Adnan说得对,模型表单集是处理这一问题的优雅方式 这是一个与您需要的内容类似的完整示例-
如本例所示,由于最终用户可能需要在您的案例中添加与产品相关的其他图像,因此您需要一些javascript逻辑来处理表单的动态添加,以便用户可以在保存时任意添加和上载图像。是的,谢谢您的回答。您能提供一些示例代码吗?我正试图让它工作,但我没有运气。我目前保存产品,使用commit=False保存表单集,循环向产品分配/保存图像-但它不起作用。它给了我一个索引器:列表索引超出范围。还有,queryset到底应该是什么?没有一个所有图像?所有属于产品的图像?我完全迷路了。你能在视图函数中添加commit=False和图像分配吗?这里有一个很好的例子,没有使用javascript,你可以参考,但请注意,你需要在html中使用多部分表单,因为你正在上传图像。参考抱歉延迟-在尝试使用模型表单集之后,我显然有了正常的表单集来工作。只有一个问题我还没有解决,我想知道你是否能给我你的意见。我希望能够在表单验证失败时重新显示图像。作为隐藏表单字段传递的图像ID将被保留,但我希望能够查询这些ID,以便在模板中重新显示。我不确定我说的有道理,请让我知道。谢谢你的帮助-这很有帮助。我正在努力清理代码-我会尽快在我的原始帖子中编辑/包括它。
@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))