#9865: "patients_guardian.pesel may not be NULL" when trying to save related
object after "is_valid()"  on "save()" method
--------------------------------------+-------------------------------------
 Reporter:  bartosak                  |       Owner:  nobody    
   Status:  new                       |   Milestone:  post-1.0  
Component:  django.contrib.formtools  |     Version:  SVN       
 Keywords:  inlineformset_factory     |       Stage:  Unreviewed
Has_patch:  0                         |  
--------------------------------------+-------------------------------------
 "patients_guardian.pesel may not be NULL" when trying to save related
 object after "is_valid()"  on "save()" method

 my models are:
 {{{
 class Patient(models.Model):
     pesel = models.DecimalField(verbose_name=_('PESEL'), max_digits=20,
 decimal_places=0, primary_key=True, unique=True)
     name  = models.CharField(verbose_name=_('Imie'), max_length=20)
     surname = models.CharField(verbose_name=_('Nazwisko'), max_length=20)
     age  = models.IntegerField(verbose_name=_('Wiek'))

     def __unicode__(self):
         return '[ %s ] - %s %s' % (self.pesel, self.surname, self.name);

     class Meta:
         verbose_name = _('Pacjent')
         verbose_name_plural = _('Pacjenci')
         ordering = ['pesel']

 class Guardian(models.Model):
     FAMILY_RELATIONSHIP = (    ('R', _('Rodzic')),
                                ('s', _('Prawny')),
                                ('o', _('Inne')),
                           )
     patient = models.ForeignKey(Patient)
     pesel = models.DecimalField(verbose_name=_('PESEL'), max_digits=20,
 decimal_places=0 ,primary_key=True, unique=True)
     name  = models.CharField(verbose_name=_('Imie'), max_length=20)
     surname = models.CharField(verbose_name=_('Nazwisko'), max_length=20)
     relationship  = models.CharField(verbose_name=_('Pokrewienstwo'),
 max_length=1, choices=FAMILY_RELATIONSHIP)

     def __unicode__(self):
         return '[ %s ] - %s %s' % (self.pesel, self.surname, self.name);

     class Meta:
         verbose_name = _('Opiekun')
         verbose_name_plural = _('Opiekunowie')
         ordering = ['patient', 'pesel']
 }}}

 and my view is:
 {{{
 @login_required
 @transaction.commit_on_success
 def new_patient_and_guardian(request):
     patient_form = modelform_factory(Patient)
     guardian_form = inlineformset_factory(Patient, Guardian, extra=1,
 max_num=1)
     patient_f=patient_form()
     guardian_f=guardian_form(clone(request.POST))
     error_message = ""
     if request.method=="POST":
         patient_f=patient_form(clone(request.POST))
         if patient_f.is_valid():
             try:
                 patient = patient_f.save()

                 guardian_f=guardian_form(clone(request.POST),
 instance=patient)
                 if guardian_f.is_valid():
                     guardian_f.save()
                     transaction.commit()
                     return
 HttpResponseRedirect(reverse('viomed.patients.views.patient_address_edit',
 args=[patient.pesel]))
             except Exception, (msg):
                 error_message = "%s" % (msg)
                 transaction.rollback()
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9865>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to