Django嵌入ManyToMany形式 [英] Django embedded ManyToMany form

查看:138
本文介绍了Django嵌入ManyToMany形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个特殊的部分,我不能想像现在该怎么做。我会尽量让自己清楚。

I need a special section into a form that I can't imagine how to do it by now. I'll try to make myself clear.

假设我有一个像销售的模型。这当然是一套销售的产品。我不想选择像ManyToMany这样的产品,我只想要有两个CharField,如Name和Price,另外还有一个Add按钮。当我填写这些字段并按添加时,将有一个ajax操作,将一行放入正下方的一个框中。同样的事情,如果我想删除其中一行,通过点击此行中的删除按钮。

Suppose that I got a model like "Sale". That's naturally a set of products being sold. I don't want to select the products like on ManyToMany, I just want to have two CharFields like "Name" and "Price", and an "Add" button just besides. When I fill these fields and press "Add", there will be an ajax action that put a row into a box just below them. Same thing if I want to remove one of the rows by clicking in the "Remove" button in this row.

这基本上是一个ManyToMany字段,我可以插入即时。

That's basically a ManyToMany field in such a way that I can insert them "on-the-fly".

我完全开放给其他解决方案。
谢谢。

I'm completely open to other solutions as well. Thanks.

推荐答案

如果它适合你,整个事情可以很容易地用javascript完成。创建/编辑/删除的处理程序。创建为GET提供一个空白表单,并验证/保存POST,将新项目添加到应该属于的销售中。编辑/删除应该相当明显。

If it suits you, the whole thing could be most easily done with javascript. Create handlers for create/edit/delete. Create renders a blank form for a GET and validates/saves for a POST, adding the new item to the Sale it should belong to. Edit/Delete should follow fairly obviously.

我可能只是使处理程序渲染html部分,使用javascript将html拉入dom中以获取GET并更改数据POST到服务器。

I would probably just make the handlers render html partials, using javascript to pull the html into the dom for GET and alter the data with POSTs to the server.

假设模型,

class Sale(models.Model):
  items = models.ManyToMany('Item', related_name='sales')
  # ...

class Item(models.Model):
  # ...

然后我们可以创建一些这样的处理程序:

Then we could create some handlers like this:

def create_item(request, saleID=""):
  sale = get_object_or_404(Sale, <ID>=saleID) # <- get the sale obj

  if request.method == 'POST':
    form = ItemForm(request.POST) # <- could take the sale here and do .add() in the save()
    if form.is_valid():
      i = form.save()
      sale.items.add(i) # <- or, add in the view here
      if request.is_ajax():
        return HttpResponse('ok')
      # redirect with messages or what have you if not ajax
  else:
    # make a blank form and whatnot
  # render the form

def edit_item(request, id|pk|slug=None):
  item = get_object_or_404(Item, slug=slug)
  if request.method == 'POST':
    # do the form is_valid, form save, return 'ok' if ajax, redirect if not ajax
  else:
    form = EditForm(instance=item)
  # render the form

def delete_item(request, id|pk|slug=None):
  if request.method == 'POST':
    # delete the item and redirect, or just return "ok" if is_ajax
  # render confirmation dialog

对于前端代码,我将使用< a href =http://api.jquery.com/load/ =nofollow> http://api.jquery.com/load/ http://api.jquery.com/jQuery .get /和 http://api.jquery.com/jQuery.post/ 等但任何java脚本框架会做。

for the frontend code, I would use some combination of http://api.jquery.com/load/ http://api.jquery.com/jQuery.get/ and http://api.jquery.com/jQuery.post/ , etc but any javascript framework will do.

这篇关于Django嵌入ManyToMany形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆