Code

Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#9401 closed (worksforme)

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

Reported by: bartosak Owned by: brosner
Component: Forms Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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()

Attachments (0)

Change History (6)

comment:1 Changed 5 years ago by brosner

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Is this still an issue in 1.0.1?

comment:2 Changed 5 years ago by bartosak

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

comment:3 Changed 5 years ago by bartosak

  • Owner changed from nobody to bartosak
  • Status changed from new to assigned

comment:4 Changed 5 years ago by bartosak

  • Owner changed from bartosak to brosner
  • Status changed from assigned to new

problem still exists :(
DB engine is SQLite

comment:5 Changed 5 years ago by bartosak

  • Resolution set to worksforme
  • Status changed from new to closed

comment:6 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.