I am using django forms to add a new objects to the db. The code I currently have is:
if request.method == 'POST':
form = MyForm(data=request.POST)
if form.is_valid():
obj = form.save()
else:
form = MyForm()
return render_to_response('reflections/add_reflection.html', {'form':form},context_instance=RequestContext(request))
The code above currently adds a new object each time the form is submitted. What I want to happen is that the object is edited the next time the save button is pressed rather than adding a new record.
How would I do this?
Use
instance_id = None
if request.method == 'POST':
try:
instance = MyType.objects.get(id=request.POST.get('instance_id'))
except MyType.DoesNotExist:
instance = None
form = MyForm(data=request.POST, instance=instance)
if form.is_valid():
obj = form.save()
instance_id = obj.id
else:
form = MyForm(instance=None)
return render_to_response('reflections/add_reflection.html', {'form':form, 'instance_id': instance_id or ''},context_instance=RequestContext(request))
Once the object is saved, pass it's id in context to page
and add it to a hidden input field inside the form as name='instance_id'.
Happy Coding.
You either need to add a separate view for editing an existing object, or - better - to add the functionality to this view. To do the latter, you could pass in an instance of the object you want to edit with your modelform to else part of your clause:
else:
if existing_obj:
form = MyForm(instance=existing_obj) #this is editing your 'existing_obj'
else:
form = MyForm() # this is creating a brand new, empty form
You'll also need to update the POST-handling bit of code too. See the example here
Related
I am trying to use same view for creating form and updating any object.
My code is as below, I tried in many ways nothing is working, since I am excluding the shof from form and adding it after form.is_valid() it makes lot of confusion. If I update it creates new object. I have two urls one without ql (create new) and one with ql (update existing), I have a class vdview which provides v.shof which needs to applied in the f.shop in form. please help fix this,
#csrf_protect
#login_required
def addmenu(request, qs, ql=None):
v = vdview(request, qs)
ctgobj = get_object_or_404(v.shopcategs, pk=ql) if ql else None # ctgobj = ShopCtg(shop=v.shof)
if ql:
form = ShopCtgForm(instance=ctgobj) # Tried ShopCtgForm(instance=ctgobj, data=request.POST)
else:
form = ShopCtgForm(data= request.POST)
if request.method == 'POST':
if form.is_valid():
f=form.save(commit=False)
f.shop = v.shof
f.save()
#form.save_m2m()
return redirect('vendor-shop', qs) #thing='%s added' %f.name)
else:
pass
#else:
# form = ShopCtgForm()
return render(request,'vendorshop.html', {'shop':v.shof, 'shopcategs':v.shopcategs, 'form': form,
'heading':'Create New Category', 'createcateg': 'createcateg', 'pkaddmenupk':'y' } )
Use try blocks to handle both scenarios. The simplified example below will look for a given model instance pk and if it doesn't find it, will assume you want to create it. try will prevent django from throwing an error if the model instance doesn't exist. Rather, it will just return the empty model form.
It does this first to render the correct form in the template (the first try block) then again in the second try block after request.method == 'POST': to submit new data or update existing data.
Views.py
from .models import Books
from .forms import BookForm
def create_and_update_book_view(request, pk):
books = Books.objects.get(id=pk)
try: # get pre-populated form with model instance data (for update)
form = BookForm(instance=books.id)
except: # If it doesn't exist, show an empty form (for create)
form = BookForm(request.POST or None)
if request.method == 'POST':
try: # Do the same as above
form = BookForm(instance=books.id)
except: # Same as above
form = BookForm(request.POST or None)
if form.is_valid():
form.save()
return render(request, "create_and_update_book_page.html", {'form':form})
Using Django 1.7.
In a Function Based View, I'm trying to get the data of the current (i.e. not yet updated) instance to do a comparison which action I need to take.
When I try to get the data using smt.field1, I'd expect to get what I have in the database for this instance, but instead I get the new, unsaved data that is in the form.
def edit_something(request, pk):
smt = get_object_or_404(Something, pk=pk)
if request.method == 'POST':
form = MyForm(instance=smt, data=request.POST)
context_dict['form'] = form
if form.is_valid():
if smt.field1 != form.cleaned_data.get('field1'):
print "field1 has changed, so I need to do Action1"
else:
print "field1 has not changed, so I need to do Action2"
form.instance.modified_by = request.user
form.save()
return HttpResponseRedirect(reverse('smt_detail', args=(smt.id,)))
else:
...
In other words, the smt.field1 == form.cleaned_data.get('field1') will always yield True and Action1 will always be taken. I don't want this.
Any idea how to elegantly solve this?
You need to use form.changed_data to get at which fields have been updated.
The instance of smt gets changed when if form.is_valid(): is called.
One solution is to get the smt.field1 variable before calling form.is_valid(), e.g.
...
form = MyForm(instance=smt, data=request.POST)
context_dict['form'] = form
orig_field1 = smt.field1
if form.is_valid():
if orig_field1 != form.cleaned_data.get('field1'):
print "field1 has changed, so I need to do Action1"
else:
print "field1 has not changed, so I need to do Action2"
...
I'm creating a form in one page, then in another page I'm trying to pull out the form (populated with the data saved in it already) and would like to make changes to it so that when I save it it overwrites the instance instead of creating another one.
def edit(request):
a = request.session.get('a', None)
if a is None:
raise Http404('a was not found')
if request.method == 'POST':
form = Name_Form(request.POST, instance=a)
if form.is_valid():
j = form.save( commit=False )
j.save()
else:
form = Name_Form( instance = a )
This is the code I have for the "editting form" view.. When I open this page the form is successfully prepopulated with all the data. However, when I make changes and save, it does not overwrite the existing instance, instead it creates a new one.
Any ideas?
Have a look here: https://docs.djangoproject.com/en/dev/ref/models/instances/#how-django-knows-to-update-vs-insert
I think this may help you.
Update:
What about trying a more "explicit" way.
Assume, id_of_Name stores only the id or pk of your model which you want to edit (I assume the model is called "Name"). Then just retrieve the id/pk from session to query your db for the model instance. Also try to directly call the save method on the form.
def edit(request):
id_of_Name = request.session.get('a', None)
if id_of_Name is None:
raise Http404('id_of_Name was not found')
instance = Name.objects.get(pk=id_of_Name)
if request.method == 'POST':
form = Name_Form(request.POST, instance=instance)
if form.is_valid():
form.save()
else:
form = Name_Form( instance = instance )
I'm misunderstanding something! If my model is not saved, it does not have an id associated with it. So if I have something like this:
views.py (inserting or editing existing info uses the same modelform)
def insert_or_modify(request, id=None):
if id is not None:
book = BookModel.objects.get(pk=id)
else:
book = BookModel()
if request.method == 'POST':
form = BookInfoForm(request.POST, request.FILES, instance=book)
if form.is_valid():
form.save()
....
return render_to_response(...)
I also have an image and use upload_to for the imagefield. There are two problems: id is None and I'm not sure how to manipulate/save the instance=book so that I would actually get an id. The second problem is the location I save my data to is way off. Even though the template tag book.img.url has the desired location of the book at http:127.0.0.1:8000/folder1/media/id/, the actual location is somewhere else:
Where I want to save my image to:
/project/folder1/media/id/
where id is the book id.
What I actually get:
/project/id/
(But 'id' becomes 'None' since it doesn't exist!)
My previous code worked. It would save to the correct location, but with this current code, it doesn't work. So the saving issue doesn't seem like it's due to settings.py since it worked previously.
EDIT: removed non-code from code formatting area
EDIT: I found out why I wasn't saving to the correct location. As it turned out, I forgot to uncomment something when I last modified settings.py. Saving to the location works now! Sorry guys!
EDIT: I think the id=None problem is caused by form.save(). If I avoid doing that and just save to the model directly, I don't have this problem.
Id assigns only on saving objects when you use autoincrement id field (default).
You can save item before handling image, and then save image.
May be you can not worry about image name - becouse django file storages dont have troubles with same image names. So if you just save file "image.png", and then save another file with name "image.png" - then it will be saved as "image_1.png"
def add_or_create(request, item_id=None):
item = get_object_or_404(BookModel, id=item_id) if item_id else None
form = BookInfoForm(request.POST or None, request.FILES or None, instance=book) # assume it is ModelForm
if form.is_valid():
book = form.save()
For the first part:
def insert_or_modify(request, id=None):
if id:
book = BookModel.objects.get(pk=id)
if request.method == 'POST':
form = BookInfoForm(request.POST, request.FILES, instance=book)
if form.is_valid():
save_book = form.save()
# use save_book as your instance of BookModel
....
else:
if request.method == 'POST':
form = BookInfoForm(request.POST, request.FILES)
if form.is_valid():
save_book = form.save()
# use save_book as your instance of BookModel
....
save_book = form.save() allows you to then use save_book as your saved instance of BookModel, and save_book.id is its id.
def create_id(instance,some_id=None):
if some_id is None:
obj=Post.objects.first()
new_id=obj.id
new_id+=1
return new_id
else:
return some_id
def pre_save_post_receiver(sender, instance, *args, **kwargs):
if not instance.id:
instance.id = create_id(instance)
pre_save.connect(pre_save_post_receiver, sender=Post)
I have created a ModelForm class to be able to create and edit database entries. Creating new entries works well, however, i dont know how to use ModelForms to edit/update an existing entry. I can instantiate a ModelForm with a database instance using:
form = MyModelForm(instance=MyModel.objects.get(pk=some_id))
However, when i pass this to a template and edit a field and then try to save it, i create a new database entry instead of updating "some_id"?
Edit1:
This is my view
def editData(request):
if request.method == 'POST':
form = MyModelForm(request.POST, request.FILES)
if form.is_valid():
editedEntry = form.save() # <-- creates new entry, instead of updating
Remember you still need to use the instance parameter when you instantiate on POST.
instance = MyModel.objects.get(whatever)
if request.method == "POST":
form = MyModelForm(request.POST, instance=instance)
...
else:
form = MyModelForm(instance=instance)
Also possible and slightly shorter:
instance = MyModel.objects.get(whatever)
form = MyModelForm(request.POST or None, instance=instance)
...