python - What are the benefits of having two models instead of one? -


i've django model

class person(models.model):     name = models.charfield(max_length=50)     team = models.foreignkey(team) 

and team model

class team(models.model):     name = models.charfield(max_length=50) 

then, add 'coach' property 1 one relationship person. if not wrong, have 2 ways of doing it.

the first approach adding field team:

class team(models.model):     name = models.charfield(max_length=50)     coach = models.onetoonefield(person, related_name='master') 

the second 1 creating new model:

class teamcoach(models.model):     team = models.onetoonefield(team)     coach = models.onetoonefield(person) 

is right ? there big difference practical purposes ? pro , cons of each approach ?

i neither, every person has team , if every team has coach, it's rather redundant circulation , unnecessary.

better add field in person called type directly more clean , direct, like:

class person(models.model):     # use _ if care i18n     types = ('member', 'member',              'coach', 'coach',)      name = models.charfield(max_length=50)     team = models.foreignkey(team)     type = models.charfield(max_length=20, choices=types) 

although consider refactoring person more generic , team have manytomany person... in case, can re-use person in other areas, cheerleaders.

class person(models.model):     # use _ if care i18n     types = ('member', 'member',              'coach', 'coach',)      name = models.charfield(max_length=50)     type = models.charfield(max_length=20, choices=types)  class team(models.model):     name = models.charfield(max_length=50)     member = models.manytomanyfield(person, related_name='master') 

make models more generic , dry, should manageable , not tightly coupled fields (unless absolutely necessary), models more future proof , not fall under migration nightmare easily.

hope helps.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -