Re: Don't understand ModelForm
thank you, Shawn, for posting your working and tested example. It answered my questions (basically the same as the original post on this thread) and got my head clear. I agree in general with the point that the Django docs are not really complete on this issue; I'd like it if the Forms/ModelForms would throw out a complete CRUD example and explain each bit of it. - dan On Jul 21, 11:23 pm, Shawn Milochikwrote: > To expand on what Dan said with a full (tested and working) example: > > In urls.py: > > (r'^partner/$', 'partner_page'), > (r'^partner/(?P\d+)/$', 'partner_page'), > > In the form tag of your template: > > > The view: > > @login_required > def partner_page(request, partner_id = None): > > if partner_id: > #existing partner > partner_instance = get_object_or_404(partner, pk = partner_id) > else: > #new partner > partner_form = partnerForm() > partner_instance = None > > if request.POST: > post_data = request.POST.copy() > partner_form = partnerForm(post_data, instance = > partner_instance) > > if partner_form.is_valid(): > the_partner = partner_form.save() > partner_id = the_partner.id > > template_file = 'partner.html' > > context = { > 'partner_form': partner_form, > 'partner_id': partner_id, > } > > return render_to_response(template_file, context, > RequestContext(request)) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
What I'm doing now is # TEMPLATE {% if modelform.instance.id %} {% endif %} # The POST part of the view method pk = request.POST.get('pk',None) if pk: model = models.House.objects.get(pk=pk) modelform = forms.MyModelForm(request.POST, instance=house) else: modelform = forms.MyModelForm(request.POST) Anyone else use this approach? gene On Aug 15, 12:07 pm, "ristretto.rb"wrote: > This is an excellent thread. I came here with the same questions as > Marc. My revelation seems to be that forms are for data, and saving > is control. And these two have separate concerns/goals. Such that > fields shouldn't be in forms for the sole reason to make control of > the form function properly. > > So you can put the id in you forms with a hidden, or in the action="" > attribute, or using some sort of JavaScript, or whatever. So, I > understand that the key should be controlled outside the scope of the > Django form support. > > But, I wonder if something like {{ form_set.management_form }} for > formsets could be used here. Perhaps something like > {{form.indentifier_key}} on a template in the form could by default > write out a hidden field of the pk of the instance. It would just be > shorthand for putting the instance in the context, and typing type="hidden" name="pk" value="{{instance.id}}"/> > > But, it could make your view work like this, so you don't have to > query up the model instance yourself. > > f = your_modelform(request.POST) > f.instance # would be the read from the DB and set for you. > > Gene > > On Jul 22, 9:23 am, Shawn Milochik wrote: > > > To expand on what Dan said with a full (tested and working) example: > > > In urls.py: > > > (r'^partner/$', 'partner_page'), > > (r'^partner/(?P\d+)/$', 'partner_page'), > > > In the form tag of your template: > > > > > The view: > > > @login_required > > def partner_page(request, partner_id = None): > > > if partner_id: > > #existing partner > > partner_instance = get_object_or_404(partner, pk = partner_id) > > else: > > #new partner > > partner_form = partnerForm() > > partner_instance = None > > > if request.POST: > > post_data = request.POST.copy() > > partner_form = partnerForm(post_data, instance = > > partner_instance) > > > if partner_form.is_valid(): > > the_partner = partner_form.save() > > partner_id = the_partner.id > > > template_file = 'partner.html' > > > context = { > > 'partner_form': partner_form, > > 'partner_id': partner_id, > > } > > > return render_to_response(template_file, context, > > RequestContext(request)) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
This is an excellent thread. I came here with the same questions as Marc. My revelation seems to be that forms are for data, and saving is control. And these two have separate concerns/goals. Such that fields shouldn't be in forms for the sole reason to make control of the form function properly. So you can put the id in you forms with a hidden, or in the action="" attribute, or using some sort of JavaScript, or whatever. So, I understand that the key should be controlled outside the scope of the Django form support. But, I wonder if something like {{ form_set.management_form }} for formsets could be used here. Perhaps something like {{form.indentifier_key}} on a template in the form could by default write out a hidden field of the pk of the instance. It would just be shorthand for putting the instance in the context, and typing But, it could make your view work like this, so you don't have to query up the model instance yourself. f = your_modelform(request.POST) f.instance # would be the read from the DB and set for you. Gene On Jul 22, 9:23 am, Shawn Milochikwrote: > To expand on what Dan said with a full (tested and working) example: > > In urls.py: > > (r'^partner/$', 'partner_page'), > (r'^partner/(?P\d+)/$', 'partner_page'), > > In the form tag of your template: > > > The view: > > @login_required > def partner_page(request, partner_id = None): > > if partner_id: > #existing partner > partner_instance = get_object_or_404(partner, pk = partner_id) > else: > #new partner > partner_form = partnerForm() > partner_instance = None > > if request.POST: > post_data = request.POST.copy() > partner_form = partnerForm(post_data, instance = > partner_instance) > > if partner_form.is_valid(): > the_partner = partner_form.save() > partner_id = the_partner.id > > template_file = 'partner.html' > > context = { > 'partner_form': partner_form, > 'partner_id': partner_id, > } > > return render_to_response(template_file, context, > RequestContext(request)) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
To expand on what Dan said with a full (tested and working) example: In urls.py: (r'^partner/$', 'partner_page'), (r'^partner/(?P\d+)/$', 'partner_page'), In the form tag of your template: The view: @login_required def partner_page(request, partner_id = None): if partner_id: #existing partner partner_instance = get_object_or_404(partner, pk = partner_id) else: #new partner partner_form = partnerForm() partner_instance = None if request.POST: post_data = request.POST.copy() partner_form = partnerForm(post_data, instance = partner_instance) if partner_form.is_valid(): the_partner = partner_form.save() partner_id = the_partner.id template_file = 'partner.html' context = { 'partner_form': partner_form, 'partner_id': partner_id, } return render_to_response(template_file, context, RequestContext(request)) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
Oh I gotcha now. Django model forms by default do not represent auto fields (ID's) because it wouldn't make much sense for a user to be able to edit the PK in a form. Having said that, I do understand why you need to be able to see the PK. I shown in my example, I personally prefer to represent the PK in the url so something like http://localhost/Partner/Edit/3 would be sent to the view: def edit(request, id): If you do not like doing it that way, you just have to pass the ID of the partner object into your template. SInce you are saving your partner object in session you can get access to it right away in the template. Just alter your template a bit: {{ form.as_Table}} Now you ahve Id available in your post data, so you can do your check. if request.POST.get("partner_id"): continue processing --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
Of course! The mistake is that I didn't have the ID in the URL, but I took the partner to be edited somewhere from the session. I'll change the code and it should work as expected. Thanks again for your patience Marc On Jul 21, 10:57 pm, Daniel Rosemanwrote: > On Jul 21, 9:34 pm, mettwoch wrote: > > > > > I'd like this to work: > > > class PartnerForm(forms.ModelForm): > > class Meta: > > model = Partner # I expect that all fields are included in > > the form (the 'id' field as well) > > > def partner_new(request): > > form = PartnerForm() > > return render_to_response('pos.html', {'form': form}, > > RequestContext(request)) > > > def partner_edit(request): > > form = PartnerForm(instance = request.session['partner']) > > return render_to_response('pos.html', {'form': form}, > > RequestContext(request)) > > > def partner_save(request): > > if request.POST.get('id', None): > > form = PartnerForm(request.POST, instance = get_object_or_404 > > (Partner, pk = request.POST['id'])) > > else: > > form = PartnerForm(request.POST) > > if form.is_valid(): > > form.save() > > return render_to_response('pos.html', {}, RequestContext > > (request)) > > else: > > return render_to_response('pos.html', {'form': form}, > > RequestContext(request)) > > > here's an excerpt of the pos.html: > > > {% if form %} > > > > > > {{ form.as_table }} > > > > > > > > ... > > > as I said: maybe I'm on the wrong track with ModelForm ... > > Well, yes. What you've posted doesn't make any sense at all. You want > to get the id from the form, so that you can get the instance from the > database to pass to the form so it can get the id! This is obviously > circular. At some point, you've got to tell the form what the id > actually is, and that means passing it in to the page where you first > render the form. If you don't have an id, you pass a blank instance, > and a new row will be made in the database. If you do have one, pass > the instance with that id, and the existing row will be edited. > > Various people have posted more or less complex code to do this, but > the simplest is like this: > > def my_form_view(request, partner_id=None): > if partner_id: > partner = Partner.objects.get(id=partner_id) > else: > partner = Partner() > > if request.POST: > form = PartnerForm(request.POST, instance=partner) > if form.is_valid(): > form.save() > return HttpResponseRedirect('/') > else: > form = PartnerForm(instance=partner) > > return render_to_response('pos.html', {'form': form}, > RequestContext(request)) > > One final thing - make your form action="." so it posts back to the > same URL, ie the one with the partner_id. > > That's all there is to it - nothing complicated at all. > -- > DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
On Jul 21, 9:34 pm, mettwochwrote: > I'd like this to work: > > class PartnerForm(forms.ModelForm): > class Meta: > model = Partner # I expect that all fields are included in > the form (the 'id' field as well) > > def partner_new(request): > form = PartnerForm() > return render_to_response('pos.html', {'form': form}, > RequestContext(request)) > > def partner_edit(request): > form = PartnerForm(instance = request.session['partner']) > return render_to_response('pos.html', {'form': form}, > RequestContext(request)) > > def partner_save(request): > if request.POST.get('id', None): > form = PartnerForm(request.POST, instance = get_object_or_404 > (Partner, pk = request.POST['id'])) > else: > form = PartnerForm(request.POST) > if form.is_valid(): > form.save() > return render_to_response('pos.html', {}, RequestContext > (request)) > else: > return render_to_response('pos.html', {'form': form}, > RequestContext(request)) > > here's an excerpt of the pos.html: > > {% if form %} > > > {{ form.as_table }} > > > > ... > > as I said: maybe I'm on the wrong track with ModelForm ... Well, yes. What you've posted doesn't make any sense at all. You want to get the id from the form, so that you can get the instance from the database to pass to the form so it can get the id! This is obviously circular. At some point, you've got to tell the form what the id actually is, and that means passing it in to the page where you first render the form. If you don't have an id, you pass a blank instance, and a new row will be made in the database. If you do have one, pass the instance with that id, and the existing row will be edited. Various people have posted more or less complex code to do this, but the simplest is like this: def my_form_view(request, partner_id=None): if partner_id: partner = Partner.objects.get(id=partner_id) else: partner = Partner() if request.POST: form = PartnerForm(request.POST, instance=partner) if form.is_valid(): form.save() return HttpResponseRedirect('/') else: form = PartnerForm(instance=partner) return render_to_response('pos.html', {'form': form}, RequestContext(request)) One final thing - make your form action="." so it posts back to the same URL, ie the one with the partner_id. That's all there is to it - nothing complicated at all. -- DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
I'd like this to work: class PartnerForm(forms.ModelForm): class Meta: model = Partner # I expect that all fields are included in the form (the 'id' field as well) def partner_new(request): form = PartnerForm() return render_to_response('pos.html', {'form': form}, RequestContext(request)) def partner_edit(request): form = PartnerForm(instance = request.session['partner']) return render_to_response('pos.html', {'form': form}, RequestContext(request)) def partner_save(request): if request.POST.get('id', None): form = PartnerForm(request.POST, instance = get_object_or_404 (Partner, pk = request.POST['id'])) else: form = PartnerForm(request.POST) if form.is_valid(): form.save() return render_to_response('pos.html', {}, RequestContext (request)) else: return render_to_response('pos.html', {'form': form}, RequestContext(request)) here's an excerpt of the pos.html: {% if form %} {{ form.as_table }} ... as I said: maybe I'm on the wrong track with ModelForm ... On Jul 21, 10:22 pm, Dan Harriswrote: > What do you mean by having the ID "in" the form? I don't quite > understand what you are trying to do. > > On Jul 21, 4:15 pm, mettwoch wrote: > > > Ok, I come back to what I wrote before. If the partner already exists > > it has an id (primary-key or whatever). If it doesn't exist it has no > > id. I'd just like to have the id in the form. Is it a bug, is > > something missing here or am I completely on the wrong track. That's > > basic database form handling. Well I could fallback to using Form > > instead of ModelForm and maybe I'll manage to get that id in the form > > as a hidden field or whatever, but I wonder how such basic things seem > > impossible with the ModelForm. > > > Thanks for Your patience > > > Marc > > > On Jul 21, 9:55 pm, Shawn Milochik wrote: > > > > On Jul 21, 2009, at 3:39 PM, mettwoch wrote: > > > > > Sorry to insist, but that's what I don't understand here. How can I > > > > know that the partner that is 'posted' is new or not? I really become > > > > crazy here. I can't believe after all the good things I've discovered > > > > in Django that this can be so hard. > > > > Okay, thanks for clarifying. > > > > Well, you can have the partner identified in the URL by a slug or > > > something, for one. That's probably the easiest way. You do need to > > > track it somehow, starting when you pull the instance from the > > > database to populate the ModelForm. Otherwise, how can you know that > > > it's someone new versus someone with the same details? > > > > The way I'm doing it is by having separate views and urls for editing > > > and creating a new entry. But in any case you have to write something > > > somewhere when you are pulling an existing record. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
On Jul 21, 2009, at 4:22 PM, Dan Harris wrote: > > What do you mean by having the ID "in" the form? I don't quite > understand what you are trying to do. > > On Jul 21, 4:15 pm, mettwochwrote: >> Ok, I come back to what I wrote before. If the partner already exists >> it has an id (primary-key or whatever). If it doesn't exist it has no >> id. I'd just like to have the id in the form. Is it a bug, is >> something missing here or am I completely on the wrong track. That's >> basic database form handling. Well I could fallback to using Form >> instead of ModelForm and maybe I'll manage to get that id in the form >> as a hidden field or whatever, but I wonder how such basic things >> seem >> impossible with the ModelForm. >> >> Thanks for Your patience >> >> Marc >> What he's referring to is the fact that, if you're using a ModelForm, the model's ID is not available in the template. And my answer to the question is that I use the ID (or a slug) in both the URL and in the form's "action" argument, so that when it's submitted, you have the info you need in the request. Just as a quick example: (r'^patient/(?P[-\w]+)/$', 'view'), This will pass the variable "slug" to the view, and the slug is used to identify the patient. If you don't use slugs, the ID works just as well (although it makes for less friendly- looking URLs). --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
What do you mean by having the ID "in" the form? I don't quite understand what you are trying to do. On Jul 21, 4:15 pm, mettwochwrote: > Ok, I come back to what I wrote before. If the partner already exists > it has an id (primary-key or whatever). If it doesn't exist it has no > id. I'd just like to have the id in the form. Is it a bug, is > something missing here or am I completely on the wrong track. That's > basic database form handling. Well I could fallback to using Form > instead of ModelForm and maybe I'll manage to get that id in the form > as a hidden field or whatever, but I wonder how such basic things seem > impossible with the ModelForm. > > Thanks for Your patience > > Marc > > On Jul 21, 9:55 pm, Shawn Milochik wrote: > > > > > On Jul 21, 2009, at 3:39 PM, mettwoch wrote: > > > > Sorry to insist, but that's what I don't understand here. How can I > > > know that the partner that is 'posted' is new or not? I really become > > > crazy here. I can't believe after all the good things I've discovered > > > in Django that this can be so hard. > > > Okay, thanks for clarifying. > > > Well, you can have the partner identified in the URL by a slug or > > something, for one. That's probably the easiest way. You do need to > > track it somehow, starting when you pull the instance from the > > database to populate the ModelForm. Otherwise, how can you know that > > it's someone new versus someone with the same details? > > > The way I'm doing it is by having separate views and urls for editing > > and creating a new entry. But in any case you have to write something > > somewhere when you are pulling an existing record. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
Ok, I come back to what I wrote before. If the partner already exists it has an id (primary-key or whatever). If it doesn't exist it has no id. I'd just like to have the id in the form. Is it a bug, is something missing here or am I completely on the wrong track. That's basic database form handling. Well I could fallback to using Form instead of ModelForm and maybe I'll manage to get that id in the form as a hidden field or whatever, but I wonder how such basic things seem impossible with the ModelForm. Thanks for Your patience Marc On Jul 21, 9:55 pm, Shawn Milochikwrote: > On Jul 21, 2009, at 3:39 PM, mettwoch wrote: > > > > > Sorry to insist, but that's what I don't understand here. How can I > > know that the partner that is 'posted' is new or not? I really become > > crazy here. I can't believe after all the good things I've discovered > > in Django that this can be so hard. > > Okay, thanks for clarifying. > > Well, you can have the partner identified in the URL by a slug or > something, for one. That's probably the easiest way. You do need to > track it somehow, starting when you pull the instance from the > database to populate the ModelForm. Otherwise, how can you know that > it's someone new versus someone with the same details? > > The way I'm doing it is by having separate views and urls for editing > and creating a new entry. But in any case you have to write something > somewhere when you are pulling an existing record. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
This may not be helpful but here is how I usually go about processing model forms: class Partner(models.Model): # this is your model, DB fields go in here # This is your model form class PartnerForm(forms.ModelForm): class Meta: model = Partner # This is the view that displays a new partner form or processes the form def new(request): form = PartnerForm() if request.method=="POST": # if there is a post, they are sending in the form so re- create the form from the post data form = PartnerForm(request.POST) if form.is_valid(): p = form.save() # save the form and store the resulting Partner object in p return HttpResponseRedirect("/success") # always redirect after processing a form to revent refresh spam # if we got to this point it means it wasn't a POST and we didn't process a form, or the form was invalid # either way, render whatever we had with errors and what not return render_to_response('new_partner_template.html', {'form': form}) # This is a view to edit and existing parter def edit(request, id): # ID is the partners identifying number such as primary key, SSN, whatever you want p = get_object_or_404(Partner, pk=id) # this will throw a 404 if they requested an invalid partner form = PartnerForm(instance=p) # create a form pre-populated with the partner data we just loaded if request.method=='POST': # now process the submitted form form = PartnerForm(request.POST, instance=p) # this creats a form using the database data AND the post data if form.is_valid(): p = form.save() return HttpResponseRedirect('/success') # if we got down here, they didn't submit the form, or they did and there were errors, so render it back return render_to_response('my_template.html', {'form': form}) This is how I do it, If you have any further questions I can try to help. Please note this code was off the top of my head, so it may not compile "out of the box" Dan --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
On Jul 21, 2009, at 3:39 PM, mettwoch wrote: > > Sorry to insist, but that's what I don't understand here. How can I > know that the partner that is 'posted' is new or not? I really become > crazy here. I can't believe after all the good things I've discovered > in Django that this can be so hard. > Okay, thanks for clarifying. Well, you can have the partner identified in the URL by a slug or something, for one. That's probably the easiest way. You do need to track it somehow, starting when you pull the instance from the database to populate the ModelForm. Otherwise, how can you know that it's someone new versus someone with the same details? The way I'm doing it is by having separate views and urls for editing and creating a new entry. But in any case you have to write something somewhere when you are pulling an existing record. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
Sorry to insist, but that's what I don't understand here. How can I know that the partner that is 'posted' is new or not? I really become crazy here. I can't believe after all the good things I've discovered in Django that this can be so hard. Thanks for your time > > You are right, I made a mistake there. I typed it off of the top of my > head, instead of copying and pasting from working code. > > All you have to do is what I did above in the create -- have an if > statement. If the partner already exists, you have to get > that object (using the Partner.objects.get(your_criteria_here) and > pass it using the 'instance' keyword. > > Something like: > > #if it's an existing partner: > partner_form = PartnerForm(instance = partner_instance) > > #else: > partner_form = PartnerForm(request.POST) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
> > This always creates a new partner! In fact, in the ModelForm > documentation is written that the save() method creates a new instance > unless an existing instance is passed to the ModelForm constructor. So > one has to differentiate if the POST comes from an initially empty > form or from a bound form! How can this be done? I believe that it > could be easily achieved if the primary-key were always included as a > hidden field. If it's empty, then 'create' otherwise 'get' & 'update'. > >> >> DELETE: >> #if you have a partner object named partner_instance >> partner_instance.delete() >> >> READ: >> What do you mean? >> Partner.objects.get(pk = 12) #or whatever You are right, I made a mistake there. I typed it off of the top of my head, instead of copying and pasting from working code. All you have to do is what I did above in the create -- have an if statement. If the partner already exists, you have to get that object (using the Partner.objects.get(your_criteria_here) and pass it using the 'instance' keyword. Something like: #if it's an existing partner: partner_form = PartnerForm(instance = partner_instance) #else: partner_form = PartnerForm(request.POST) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
Hi again, That's exactly what I did and the point is that it doesn't work! See my comments below ... On Jul 21, 8:00 pm, Shawn Milochikwrote: > On Jul 21, 2009, at 12:49 PM, mettwoch wrote: > > > > > Sorry my old brain doesn't get it. Is there somewhere a complete CRUD > > example using ModelForm? > > > Thanks > > Marc > > CRUD: > > If your modelform is called PartnerForm, then. > > CREATE and UPDATE: > > #on initial page load (no POST) > > #if you have a partner object already (named partner_instance): > partner_form = PartnerForm(instance = partner_instance) > #otherwise: > partner_form = PartnerForm() > > #in the POST portion of your view > partner_form = PartnerForm(request.POST) > if partner_form.is_valid(): > partner_form.save() This always creates a new partner! In fact, in the ModelForm documentation is written that the save() method creates a new instance unless an existing instance is passed to the ModelForm constructor. So one has to differentiate if the POST comes from an initially empty form or from a bound form! How can this be done? I believe that it could be easily achieved if the primary-key were always included as a hidden field. If it's empty, then 'create' otherwise 'get' & 'update'. > > DELETE: > #if you have a partner object named partner_instance > partner_instance.delete() > > READ: > What do you mean? > Partner.objects.get(pk = 12) #or whatever In my specific case the partner is stored in the session and it can be updated. When a new partner is created it is stored in the session. There's always an 'actif' partner. > > #or, from the modelform instance, assuming > #something like partner_form = PartnerForm(instance = > partner_instance) > #has happened: > if partner_form.is_valid(): > partner_name = partner_form.cleaned_data['name'] > > I hope this helps. If not, please be more specific. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
On Jul 21, 2009, at 12:49 PM, mettwoch wrote: > > Sorry my old brain doesn't get it. Is there somewhere a complete CRUD > example using ModelForm? > > Thanks > Marc CRUD: If your modelform is called PartnerForm, then. CREATE and UPDATE: #on initial page load (no POST) #if you have a partner object already (named partner_instance): partner_form = PartnerForm(instance = partner_instance) #otherwise: partner_form = PartnerForm() #in the POST portion of your view partner_form = PartnerForm(request.POST) if partner_form.is_valid(): partner_form.save() DELETE: #if you have a partner object named partner_instance partner_instance.delete() READ: What do you mean? Partner.objects.get(pk = 12) #or whatever #or, from the modelform instance, assuming #something like partner_form = PartnerForm(instance = partner_instance) #has happened: if partner_form.is_valid(): partner_name = partner_form.cleaned_data['name'] I hope this helps. If not, please be more specific. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
Sorry my old brain doesn't get it. Is there somewhere a complete CRUD example using ModelForm? Thanks Marc On Jul 21, 6:04 pm, Shawn Milochikwrote: > On Jul 21, 2009, at 12:00 PM, mettwoch wrote: > > > > > > > Hi, > > > I'd like to implement a simple "create" & "update" form for my > > "Partner" model using ModelForm. How can I make the difference in a > > view "save" function whether the POST comes from a "creation" or an > > "update" form? Unfortunately the primary-key seems never to be > > included by default in the form fields! This would have made it easy. > > > Got 3 view functions: > > new(request): > > > > > edit(request, partner): > > > > > save(request): > > > update partner > > else: > > create partner > > > Many thanks for any hint > > Marc > > You don't need the save version. Your edit form will save the object > instance whether it's new or not, with the .save() method. This is one > of the nice things about Django models -- it simplifies things. If you > need to do something differently depending on whether the object is > being saved for the first time, you do it by overriding the save() > method in the model itself. > > Shawn --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Don't understand ModelForm
On Jul 21, 2009, at 12:00 PM, mettwoch wrote: > > Hi, > > I'd like to implement a simple "create" & "update" form for my > "Partner" model using ModelForm. How can I make the difference in a > view "save" function whether the POST comes from a "creation" or an > "update" form? Unfortunately the primary-key seems never to be > included by default in the form fields! This would have made it easy. > > Got 3 view functions: > new(request): > > > edit(request, partner): > > > save(request): >update partner >else: >create partner > > Many thanks for any hint > Marc > > > You don't need the save version. Your edit form will save the object instance whether it's new or not, with the .save() method. This is one of the nice things about Django models -- it simplifies things. If you need to do something differently depending on whether the object is being saved for the first time, you do it by overriding the save() method in the model itself. Shawn --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Don't understand ModelForm
Hi, I'd like to implement a simple "create" & "update" form for my "Partner" model using ModelForm. How can I make the difference in a view "save" function whether the POST comes from a "creation" or an "update" form? Unfortunately the primary-key seems never to be included by default in the form fields! This would have made it easy. Got 3 view functions: new(request): edit(request, partner): save(request): http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---