Django embedded ManyToMany form -
i need special section form can't imagine how now. i'll try make myself clear.
suppose got model "sale". that's naturally set of products being sold. don't want select products on manytomany, want have 2 charfields "name" , "price", , "add" button besides. when fill these fields , press "add", there ajax action put row box below them. same thing if want remove 1 of rows clicking in "remove" button in row.
that's manytomany field in such way can insert them "on-the-fly".
i'm open other solutions well. thanks.
if suits you, whole thing done javascript. create handlers create/edit/delete. create renders blank form , validates/saves post, adding new item sale should belong to. edit/delete should follow obviously.
i make handlers render html partials, using javascript pull html dom , alter data posts server.
assuming models,
class sale(models.model): items = models.manytomany('item', related_name='sales') # ... class item(models.model): # ...
then create handlers this:
def create_item(request, saleid=""): sale = get_object_or_404(sale, <id>=saleid) # <- sale obj if request.method == 'post': form = itemform(request.post) # <- take sale here , .add() in save() if form.is_valid(): = form.save() sale.items.add(i) # <- or, add in view here if request.is_ajax(): return httpresponse('ok') # redirect messages or have if not ajax else: # make blank form , whatnot # render form def edit_item(request, id|pk|slug=none): item = get_object_or_404(item, slug=slug) if request.method == 'post': # form is_valid, form save, return 'ok' if ajax, redirect if not ajax else: form = editform(instance=item) # render form def delete_item(request, id|pk|slug=none): if request.method == 'post': # delete item , redirect, or return "ok" if is_ajax # render confirmation dialog
for frontend code, use combination of http://api.jquery.com/load/ http://api.jquery.com/jquery.get/ , http://api.jquery.com/jquery.post/ , etc javascript framework do.
Comments
Post a Comment