Reputation: 317
here is the deal, this is my model:
class Health_plan(models.Model):
a = models.IntegerField ()
b = models.IntegerField ()
c = models.IntegerField ()
class Doctors_list(models.Model):
specialty = models.CharField(max_length=15)
name = models.CharField(max_length=30)
hp_id = models.ManyToManyField(Health_plan)
location = models.CharField(max_length=15)
def __unicode__(self):
return self.name
So, i have a table named Doctors_list, a doctor has one specialty, one location, one name, and a list of health plans covered.
To manage this, i have a table with the health plans on the columns (a,b,c). The doctors are identified in this list by their id, and the rows contain 1 or 0, 1 for health plan covered, 0 for not covered. like this:
ID A B C
1 0 0 1
2 1 0 0
3 1 0 1
Is there a better way to make this relation???
The user first chooses the specialty, my form:
class SpecForm(ModelForm):
a = Doctors_list.objects.values_list('specialty', flat=True)
unique = [('---------------','---------------')] + [(i,i) for i in set(a)]
specialty = forms.ChoiceField(choices=unique)
class Meta:
model = Doctors_list
The big thing is: a smart select for the relation specialty/health_plans. The user chooses the specialty. The specialty is covered by one or more doctors. I use those doctor(s) id, and go check on health_plan table wich plans are available. The user select's the plan.
I will keep researching but any tip is gratefully welcomed.
I hope i made myself clear.
Upvotes: 0
Views: 1049
Reputation: 1195
What you describe is a misuse of the ManyToMany relation. If you want to create a many-to-many relation between A and B, all you have to do is add a ManyToMany field in the definion of one of the models (A or B) and django will take care of the rest. Specifically, it will create a 3rd table where it will store the associations in the form of (A.id, B.id). This is standard rational database procedure for this type of relations, and Django is implementing it.
See http://en.wikipedia.org/wiki/Junction_table for the general definition of a many to many relation.
https://docs.djangoproject.com/en/1.5/ref/models/fields/#manytomanyfield - for the Django implementation.
Upvotes: 1