Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#9401 closed (worksforme)

inlineformset_factory raise IntegrityError Exception when correct data (is_valid() method) is ok

Reported by: Wojciech Bartosiak Owned by: Brian Rosner
Component: Forms Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Exception Type: IntegrityError at /patients/new/
Exception Value: patients_guardian.pesel may not be NULL

Environment:

Request Method: POST
Request URL: http://localhost:8080/patients/new/
Django Version: 1.0-final-SVN-8985
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.admin',
 'viomed.epikryza',
 'viomed.patients']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.csrf.middleware.CsrfMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'django.middleware.transaction.TransactionMiddleware')

POST

surname  			u'dshf'
name 				u'hsdfdsfh'
guardian_set-0-pesel 		u'564865486548'
guardian_set-TOTAL_FORMS	u'1'
age 				u'324'
guardian_set-0-relationship 	u'R'
guardian_set-0-name 		u'hdfsg'
guardian_set-INITIAL_FORMS 	u'0'
guardian_set-0-surname 		u'hdfs'
csrfmiddlewaretoken 		u'19650c559d0c0f113faba870e4b30d23'
pesel 				u'234534253452'

Models:

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']

Source code :

@login_required
@transaction.commit_manually
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()
    
    if request.method=="POST":
        patient_f=patient_form(clone(request.POST))
        if patient_f.is_valid():
            try:
                patient = patient_f.save(commit=False)
                guardian_f=guardian_form(clone(request.POST), instance=patient)
                if guardian_f.is_valid():
                    guardian_f.save()
                patient.save()
                transaction.commit()
                return HttpResponseRedirect(reverse('viomed.patients.views.patient_address_edit', args=[patient.pesel]))
            except Exception, e:
                transaction.rollback()
                raise e
        
    return render_to_response('patients/patient_form.html',
                               {
                                    'patient_f': patient_f,
                                    'guardian_f'  : guardian_f,
                               },
                               context_instance=RequestContext(request))

Exception is raised at line: guardian_f.save()

Change History (6)

comment:1 by Brian Rosner, 15 years ago

Is this still an issue in 1.0.1?

comment:2 by Wojciech Bartosiak, 15 years ago

I will check with the last SVN version ASAP. Probably tommorow because im moving to new flat today

comment:3 by Wojciech Bartosiak, 15 years ago

Owner: changed from nobody to Wojciech Bartosiak
Status: newassigned

comment:4 by Wojciech Bartosiak, 15 years ago

Owner: changed from Wojciech Bartosiak to Brian Rosner
Status: assignednew

problem still exists :(
DB engine is SQLite

comment:5 by Wojciech Bartosiak, 15 years ago

Resolution: worksforme
Status: newclosed

comment:6 by (none), 15 years ago

milestone: post-1.0

Milestone post-1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top