Re: multiple forms for same model

2010-08-17 Thread Alex Robbins
contract_form.is_valid() looks at the data that was bound to the form.
This is normally the data in request.POST. It doesn't matter what a
form's initial data happened to be. It only matters what data you POST
back to it. If someone doesn't modify the form, then the initial and
the POSTed data are probably the same. However, if your form doesn't
have the read-only fields displayed in the template, they won't be
POSTing their data back.

Alex

On Tue, Aug 17, 2010 at 3:03 AM, Mess  wrote:
> Thx both for helping, I guess there isn't a straight forward solution,
> I'll prob add the missing fields manually.
>
> However I still don't get why contract_form.is_valid() doesn't return
> true since contract_form still contains the correct data when viewing
> in browser.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: multiple forms for same model

2010-08-17 Thread Mess
Thx both for helping, I guess there isn't a straight forward solution,
I'll prob add the missing fields manually.

However I still don't get why contract_form.is_valid() doesn't return
true since contract_form still contains the correct data when viewing
in browser.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: multiple forms for same model

2010-08-16 Thread Alex Robbins
I think the problem is that your form doesn't have all the required
data. When you assign the instance, you provide initial data, and
something for form.save to modify. However, when the data is posted
back, It looks like maybe the non-editable fields aren't in the POST
data. You could add the fields you don't want edited as readonly on
the front end, then do validation like this:
http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-be#answer-331550

I'm only guessing since I don't think you showed your forms.py or
template code.

Hope that helps,
Alex

On Aug 16, 8:14 am, Mess  wrote:
> Sorry, I had cut out some unimportant parts of the view causing that
> error. The update_contract is called from another method only if it is
> a POST.
>
> My main issue is that it works as it should when logged in as admin
> (probably because all the fields are in the admin version of the
> form). But when logged in as external user and using different form
> with only some of the fields, I get missing fields error. But the
> contract_form still contains the data from all the fields.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: multiple forms for same model

2010-08-16 Thread Mess
Sorry, I had cut out some unimportant parts of the view causing that
error. The update_contract is called from another method only if it is
a POST.

My main issue is that it works as it should when logged in as admin
(probably because all the fields are in the admin version of the
form). But when logged in as external user and using different form
with only some of the fields, I get missing fields error. But the
contract_form still contains the data from all the fields.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: multiple forms for same model

2010-08-16 Thread Scott Gould
You'll want to call is_valid() only if the request.method is "POST",
plus your code leaves the possibility of an error if none of those
permissions is found (your elif should probably simply be an else?).

On Aug 16, 6:49 am, Mess  wrote:
> Hi,
>
> I have a model where different users should have different
> possibilities to edit, i.e. admin should be able to edit everything,
> but users should only be able to edit certain fields.
>
> My problem is that the user form doesn't validate when submitting
> update as a user. But I get the error that the fields are missing even
> though I can see them redisplayed.
>
> I am looking at this 
> tutorial:http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a...
> And I don't get what is different in my example since I pass "instance
> = c" which contains the missing fields.
>
> def update_contract(request,c):
>         if request.user.has_perm("contracts.is_admin") or
> request.user.has_perm("contracts.is_internal"):
>             contract_form = ContractForm(request.POST, instance = c)
>         elif
> request.user.has_perm("contracts.is_external"):
>               contract_form = ContractFormExternal(request.POST,
> instance = c)
>         if contract_form.is_valid():
>             contract_form.save()
>             return view_contract_details(request, c.pk, True)
>         else:
>             return render_to_response('contracts/
> view_contract_details.html', {
>                 'info_message' : 'Please correct the data in the
> form',
>                 'contract_form': contract_form,
>                 'contract_id' : c.pk,
>             },context_instance=RequestContext(request))
>
> Anyone can hint as to where it goes wrong or a hint to how to better
> accomplish this?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



multiple forms for same model

2010-08-16 Thread Mess
Hi,

I have a model where different users should have different
possibilities to edit, i.e. admin should be able to edit everything,
but users should only be able to edit certain fields.

My problem is that the user form doesn't validate when submitting
update as a user. But I get the error that the fields are missing even
though I can see them redisplayed.

I am looking at this tutorial:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form
And I don't get what is different in my example since I pass "instance
= c" which contains the missing fields.

def update_contract(request,c):
if request.user.has_perm("contracts.is_admin") or
request.user.has_perm("contracts.is_internal"):
contract_form = ContractForm(request.POST, instance = c)
elif
request.user.has_perm("contracts.is_external"):
  contract_form = ContractFormExternal(request.POST,
instance = c)
if contract_form.is_valid():
contract_form.save()
return view_contract_details(request, c.pk, True)
else:
return render_to_response('contracts/
view_contract_details.html', {
'info_message' : 'Please correct the data in the
form',
'contract_form': contract_form,
'contract_id' : c.pk,
},context_instance=RequestContext(request))


Anyone can hint as to where it goes wrong or a hint to how to better
accomplish this?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.