views:

828

answers:

1

There are several questions about more or less same issue in stackoverflow, but none of them seems to cover the issues i can foresee. Since my django knowledege is limited i might be overreacting... so..

What i want to accomplish, with django, is to edit 2 models, List and ListItem in same view. List as common form and listitem as inlineformset. Creating those two forms is not a problem. Passing them into a view > template is not a problem either.

What i am worried about is,

1) How to post them to same address and handle that post request.

2) How to set up adding / removing those inline ListItems from this formset with javascript.

2.1) if i send ajax request to some address to remove ListItem object and then use javascript to remove that part of the form. How will the django view part know, which object to delete from the database? Do i need to pass ID of the object to template, so i can send it back to view with ajax?

2.2) If the whole changed ListItem inline formset gets posted after i have removed/added rows, then wont it cause problems, because the dictionary of objects that was used initially has been changed in between?

3)any other pitfalls someone could see?

Btw i dont want through code examples. If you can, just explain how things work and what i should do and keep in mind. If you know of up to date examples, then i could use some links too.

Edit (and answer to my own questions): I tried it and here is what i found: 1) Just pos them... in view you can do it like this:

form = ListForm(request.POST, instance=l)
formset = ShoppingListFormSet(request.POST, instance=l)

And then do whatever is needed - very simple and easy.

2)Duplicate existing row/form or remove one. All you need to keep in mind is that element names are correct and the stuff that formset loads with {{ form.management_form }} contains up to date and correct information with how many forms there is. All field names also need to be up to date. If you remove form from between forms 1 and 3, then form 3 numbers need to be changed to 2 and so on.

2.1) element id can be extracted from {{ form.initial.id }} and then used in form

2.2) No if form.management_form info is correct and if field names are up to date (see 2.)

3) Just have to build views, so your site will not be ruined by cross site request forgery (see http://docs.djangoproject.com/en/dev/ref/contrib/csrf/)

Alan.

+1  A: 

The way you describe doing it is fine. I actually just posted a similar answer to another Django question which you can see here -- http://stackoverflow.com/questions/1405587/django-add-remove-form-without-multiple-submit/1406819#1406819 The difference there of course being that there is not javascript to do the fancy AJAX and client-side HTML modification.

The basic gist of the REST as mentioned above (over simplified, i know) is that everything is accessible by a URL. The way "Django knows which record to delete" is that for each URL, it has a given view mapped to it. It's the view that receives the record ID and then knows which one to delete.

If you haven't already started toying with AJAX, I'd recommend looking into Jquery as it provides some very easy to use wrappers around the XML request objects built into the browsers.

By the way, just a recommendation since it sounds like you are new to this -- Get everything working WITHOUT ajax first (even if doing actions results in a white screen or poor interface), then add ajax second. It's easily to debug when you're not fussing with AJAX, and then to add AJAX after you know it all works on the server side.

T. Stone
Thanks for explaining REST. I had thought about handling formset element removal the way you explained in your other answer, but i did not put that togehter with REST. Suggesting that i do it without ajax first is also very sensible and that is exactly the way i will do it. Thanks!I have toyed with ajax calls before and have found jquery work amazingly well with django so far.But all this still does not answer my most serious concerns - how to handle posting form and inline formset on same url and what happens....
Zayatzz
... when this formset loads 3 items to the template. If i use non ajax call to remove one of those froms, then i will probably do it like this, do request on url, that contains all necessary info to understand, that i want to delete one ListItem object, it deletes it and sends me back to previous view/url. This creates updated queryset for this inline formset.But since, as i understand, ajax call just does deleting and js removes object from template, but the queryset does not get updated. What happens then? Am i overcomplicating things and django just 'takes care of it'?
Zayatzz
Ok i went ahead and just tested how posting multipleforms to same address works and it works just as posting single form. Who would have thought it is ths easy :P. Now just adding and deleting rows remains to be tested.
Zayatzz