Atom
Atom

Reputation: 15

ManytoManyField Django relationships error

I don't understand why I get an error when I try to view the allergies that I added to my patient ?

def create_allergie(request):
    if request.method == 'POST':
        try:
            print 'In create_allergie()'
            allergy = Allergie()
            allergy.name = request.POST['name_allergie']
            allergy.id_allergie = 1065
            allergy.intensity = 10
            allergy.add_by = int(request.POST['user_pk'])
            allergy.deleted = False
            allergy.deleted_date = '20-09-2014:20:10:05'
            allergy.deleted_by = 5
            allergy.save()
            print '-- ALLERGIE SAVE --'
            pk_patient = int(request.POST['patient_pk'])
            my_patient = get_object_or_404(Patient, pk=pk_patient)
            print my_patient
            allergy.patient.add(my_patient)
            print '---- DISPLAY TABLE ----'
            print 'PATIENT : ', allergy.patient.all()
            print 'ALLERGY : ', my_patient.allergie_set.all()
        except Exception, b:
            print b
            raise b
        return render_json([])

I put here the output I get :

In create_allergie()

-- ALLERGIE SAVE --

Mobile Joee

---- DISPLAY TABLE ----

PATIENTS : [Patient: Mobile Joee]

ALLERGIES : 'Allergie' object has no attribute 'content'

I also give you the model 'Allergie' :

class Allergie(models.Model):
    name = models.CharField(max_length=25, verbose_name="Nom de l'allergie")
    id_allergie = models.IntegerField()
    intensity = models.IntegerField()
    add_date = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name="Date d'ajout de l'allergie")
    add_by = models.IntegerField()
    deleted = models.BooleanField()
    deleted_date = models.CharField(max_length=255, verbose_name="Date de suppression de l'allergie")
    deleted_by = models.IntegerField()
    patient = models.ManyToManyField(Patient)

def __unicode__(self):
    return unicode(self.content)

class Meta:
    verbose_name = "Allergie"
    verbose_name_plural = "Allergies"

I hope someone can help me ... Thanks

Upvotes: 0

Views: 49

Answers (1)

cezar
cezar

Reputation: 12012

The error is in your def __unicode__. The error you get says that 'Allergie' object has no attribute 'content'. When printing the allergies, Django tries to print what is defined in def __unicode__. This method generates the error. You can maybe change it to:

def __unicode__(self):
    return self.name

Upvotes: 1

Related Questions