Hi Mike,
Make sure you are setting the instance argument when you instantiate the form.
Typically, you would pass in a pk to the view.
def my_update_view(request, pk):
obj = get_object_or_404(MyModel, pk=pk)
form = MyForm(request.POST or None, instance=obj)
….
I hope this helps you.
Best wishes,
Matthew
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/33b0c293-f017-476d-a6b1-81223f6c863c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I see. We are using Bootbox for our modals, but I’m sure the concept is the same.
We have a separate template of the form that we include when we initialize the page. We have an event listener on the modal button, so that when it is clicked, it replaces the HTML inside the form with the HTML returned by our ajax view. In order to get a specific object, we use the querystring to pass the PK of the object to retrieve in the ajax view. We have a save button on our modal form, and that POSTs back to the same ajax view. If it does not validate, we return the HTML of the form with validation errors; otherwise, we send back a success message so that the event handler on the save button can close the modal or reload the page or do something else.
That is probably as clear as mud…
From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of Michael Chenetz
Sent: Friday, March 3, 2017 2:37 PM
To: Django users
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
django-users...@googlegroups.com.
To post to this group, send email to
django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/24087ac2-5f69-4818-82a1-842fd0c52830%40googlegroups.com.
Mike
Hi Michael,
Disclaimer: leaving the Javascript side of things out of scope.
On Friday 03 March 2017 12:07:16 Michael Chenetz wrote:
> I am trying to figure out how to populate the update model form in a
> Bootstrap Modal. I can add to a form no problem but update does not
> seem to pre-populate the form when it is viewed.
Look at how ModelFormMixin does it:
So, in order to prepopulate the form in the modal you need to provide the instance argument and assign it the model instance. I assume you know what model instance you need to update.
--
Melvyn Sopacua
On Sunday 05 March 2017 13:40:44 Michael Chenetz wrote:
> Thanks Melvyn... I am not having issues with a generic update form.
The important stuff is in the last line. I was explaining how you can do it and where to look for inspiration.
> The problem is when using it in a Bootstrap Modal. I think it is a
> javascript/ajax issue. That is the piece I need to figure out.
That depends on your workflow. You can fill the form using an Ajax call, yes. But - you don't have to.
The above is part of code I'm working on. I am actually working on handling duplicated form media in the AdditionalFormsManager, so it certainly isn't production quality yet - but it will work just fine for illustration purposes or base for your own code.
--
Melvyn Sopacua