Reputation: 8101
I have the following situation. My Django project has the following model
class Doctor(models.Model):
#some doctor specific fields
I also have a model tha was like this
class Category(models.Model):
name = models.CharField(max_length = 15)
color = models.CharField(max_length = 7)
So each category has a name and a specific color. I was asked to do the following. Each doctor to have his own phrase but the colors are always the same. I am not sure that this is possible. Is it? If I do this
class Doctor(models.Model):
#some doctor specific fields
category = models.ForeignKey(Category)
then each doctor could have his own specific categories but for the same category we might have different colors. Can this be done on db level? Or must I do it programmatically? If it is done programmatically my db should check at save time or while typing if category allready exists and if it does to automatically chose the color of the category correct?
What would be the best way to implement this? As I think of it needs both front end and back end verification one for the frontend while typing and one for the back end checking the db to see if category allready exists. But I don't like how this sounds.
Upvotes: 0
Views: 27
Reputation: 8539
What if you had three models? i.e.:
class Doctor(models.Model):
#some doctor specific fields
category = models.ForeignKey(Category)
class Category(models.Model):
name = models.CharField(max_length = 15)
color = models.ForeignKey(CategoryColor)
class CategoryColor(models.Model):
color = models.CharField(max_length = 7)
Then each Doctor would have a Category, and each Category would have one CategoryColor. If the Category already exists, you would use the existing one and its associated CategoryColor, otherwise it would create a new Category and corresponding CategoryColor.
Upvotes: 1