hi,
my scenario as below:
- I want to copy existing record and show at the form. then clear the pk
and id then it will be able to "add" instead of "update".
- all working fine. but, if the record is exist (Unique error). it should
prompt unique key 's error. but, in the form.is_valid
has no trigger the error. untill the cmodel.save() only it throw
exception. but, the form doesn't render the error message.
if without using "form = ProgramTypeForm(request.POST or
None,instance=dataObject)" and use "form = ProgramTypeForm(request.POST or
None)",
then, it will be false if we run form.is_valid() else, it will True if we
execute form.is_valid() , or maybe my method are incorrect?
below are the coding:
______________________________________________________________
models.py
______________________________________________________________
class ProgramType(Standard):
pt_type = models.CharField('Program Type',
max_length=20,blank=False,unique=True)
pt_desc = models.CharField('Program Description',max_length=100)
def __unicode__(self):
return self.pt_type
class Meta:
unique_together = (('pt_type'),)
______________________________________________________________
views.py
def form_copy(request,id_key):
dataObject = get_object_or_404(ProgramType,pk=id_key)
dataObject.pk = None
dataObject.id = None
form = ProgramTypeForm(request.POST or None,instance=dataObject)
if form.is_valid():
try:
cmodel = form.save(commit=False)
cmodel.id = None
cmodel.pk = None
cmodel.save() # <============ *here is the error*
except:
# do exception
finally:
# do finally
else:
# return form invalid render.
______________________________________________________________
forms.py
class ProgramTypeForm(ModelForm):
pt_desc = forms.CharField(widget=forms.Textarea(attrs = {'class':
"page-text", 'cols': 30, 'rows': 10}), label='Description')
class Meta(CommonForm.Meta):
model = ProgramType
Regards,
MH
--
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.