python - Edit multiple objects with Get() function Django -
i want edit 2 forms in django. form 'motel' , 'images'. in app, users can upload multiple images 'motel' model. , now, editing images 'get()' function giving me,
multipleobjectsreturned get() returned more 1 motelimages -- returned 4!
models
class motel(models.model): user= models.foreignkey(user) title= models.charfield(max_length=120) body= models.textfield() #other fields class motelimages(models.model): motel= models.foreignkey(motel, default=none, related_name='images') image= models.imagefield(upload_to='company', verbose_name= 'image') class motelimagesform(forms.modelform): image= forms.imagefield(label='image',) def __init__(self, *args, **kwargs): super(motelimagesform,self).__init__(*args, **kwargs) self.fields['image'].widget= forms.fileinput(attrs={'name':'image', 'multiple':'multiple'})
views saving form
def create_motel(request): if request.method=="post": motelform= motelform(request.post, request.files) formset=motelimagesform(request.post, request.files) if motelform.is_valid() , formset.is_valid(): human= true motel_form= motelform.save(commit=false) motel_form.user= request.user motel_form.pub_date= datetime.datetime.now() motel_form.save() image in request.files.getlist('image',[]): photo= motelimages(motel=motel_form, image=image) photo.save() messages.success(request, "welldone boy") return httpresponseredirect('/view_all/') else: print motelform.errors, formset.errors else: motelform=motelform() formset= motelimagesform() return render(request, 'motels/add_motel.html',{'motelform': motelform, 'formset':formset})
views editing form
@login_required def edit_motel(request,motel_id=none,slug=none,template_name='motel_edit.html'): if id: post=get_object_or_404(motel,id=motel_id,slug=slug) images=motelimages.objects.get(motel=motel_id) else: post=motel(user=request.user) images=motelimages(user=request.user) if request.post: motelform=motelform(request.post,request.files, instance=post) formset=motelimagesform(request.post,request.files, instance=images) if form.is_valid() , formset.is_valid(): form.save() formset.save() redirect_url=reverse('moteldetail',kwargs={'motel_id':motel_id,'slug':slug}) return render(request, 'motels/updatenotice.html') else: form=motelform(instance=post) formset= motelimagesform(instance=images) return render(request, template_name,{'formset':formset,'motelform':motelform})
template editing form
<form id="post_form" method="post" action="" enctype="multipart/form-data"> {% csrf_token %} {% hidden in motelform.hidden_fields %} {{ hidden }} {% endfor %} {% field in motelform %} {{ field.name }} {{ field }} <br /> {% endfor %} {{ formset.management_form }} {% form in formset %} {{ form }} {% endfor %} <input type="submit" name="submit" value="submit" /> </form>
what missing?
images = motelimages.objects.get(motel=motel_id)
since get
can return 1 object, want use filter
:
images = motelimages.objects.filter(motel__pk=motel_id)
images
list of relevant images.
Comments
Post a Comment