Thursday, July 29, 2010

through/unique_together usage

Hi,

In my application I want to enforce a each-image-can-only-be-assigned-
to-a-single-patient and believed I'd be able to do this by adding a
unique_together=("patient", "image") to the PatientImageRecord
definition - summary of my models.py:

class ImageCatagory(models.Model):
...

class ImageRecord(models.Model):
...

class Patient(models.Model):
...
images = models.ManyToManyField(ImageRecord, blank=True,
through="PatientImageRecord")

class PatientImageRecord(models.Model):
patient = models.ForeignKey(Patient)
image = models.ForeignKey(ImageRecord)

Before adding the through=... clause my admin pages worked fine
including joins to the referenced images, but one could assign the
same image to more than one patient! Now that I have defined my own
model, PatientImageRecord, linking the two, the Patient admin page
breaks with the message "'NoneType' object has no attribute 'label''
unless I remove images from the page.

Any ideas what I'm doing wrong? Is this the right way to enforce the
desired relationship?

Thanks
Dave

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment