I'm not sure is you're talking about UPDATE or INSERT.
Clearly when INSERTing new record it's easy to just setdefaults. The
problem arises when UPDATEing. If my tests are correct (I'm pretty
convinced tey are...) in fact you need to put all
fields you don't want to update as hidden fields, and that's beyond my
comprehension.
I also noticed that all missing fields are tested even if they are
present in the record in the db and even if the view will anyhow get
the record from the db. So Why should I force the template to put
hidden values for what I already have?
In fact, while rewriting generic views for newforms I added some rows
that get the item, check for any missing fields from the POST and use
the values retrieved from the db to fill in the form before calling
form.is_valid():
def update_object(request, model, form=None, object_id=None,
slug=None,...)
....
data = MultiValueDict()
for f in object._meta.fields:
data.setlist(f.name, [f.value_from_object(object)])
for f in object._meta.many_to_many:
data.setlist(f.attname, [x.id for x in
f.value_from_object(object)])
if request.POST:
## IMPORTANT: we don't want to overwrite values that are not
from POST
for i in request.POST:
data.setlist(i, request.POST.getlist(i))
if model._meta.has_field_type(FileField):
data.update(request.FILES)
form = Form(data)
if form.is_valid():
object = form.save()
This seems to work pretty well for me.
Is there some reason I'm missing for not going this way?
sandro
*:-)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---