2014-10-09 83 views
0

我不明白爲什麼當我嘗試查看我向患者添加的過敏症時出現錯誤?ManytoManyField Django關係錯誤

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

我把這裏的輸出我得到:

在create_allergie()

- ALLERGIE SAVE -

移動Joee

----顯示錶 - ---

患者:[患者:手機喬伊]

過敏: 'Allergie' 對象有沒有屬性 '內容'

我也給你的模型 '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" 

我希望有人能幫助我...謝謝

回答

1

錯誤在您的def __unicode__。你得到的錯誤說'Allergie'對象沒有屬性'內容'。打印過敏時,Django會嘗試打印def __unicode__中定義的內容。此方法會生成錯誤。你也許可以將它改爲:

def __unicode__(self): 
    return self.name 
+0

Ohhh是@cezar,你讓我開心,多謝了很多傢伙! – Atom 2014-10-09 09:22:52