On Wednesday 18 January 2017 14:26:52 Benjamin Dauer wrote:
> Hi Melvyn,
> 
> That's exactly what I'm trying to do, but prior to making the changes
> it wasn't prepopulating. At each stage I tested and was getting blank
> fields albeit based off of the correct model.
> 
> Maybe it's because I don't provide the pk in the URL? I was trying to
> avoid that.

Gotcha! By default the class-based generic views provide two ways to identify 
the 
instance:
- pk
- slug

Look at slug_field and slug_url_kwarg here.[1]

But in the end, this is passed to get_object() (method also shown on that handy 
page) 
and this shall return the instance to be shown.
The tricky part about passing this in a form, is that you're fighting the 
order: the form 
is processed after the instance is loaded.

The best way to go is to put something in the URL that uniquely identifies the 
object 
and then override get_object() to work with that. Note that little added 
security is 
gained from hiding the PK, but if user-friendly URLs is what you're after, 
slugs (or some 
other field(s) with unique=True) are the way to go.

-- 
Melvyn Sopacua

--------
[1] 
https://ccbv.co.uk/projects/Django/1.10/django.views.generic.edit/UpdateView/

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/17336152.2CZh43FIzZ%40devstation.
For more options, visit https://groups.google.com/d/optout.

Reply via email to