should i use django's many-to-many option? -
django newb here, working off django polls tutorial , trying add model that'll keep track of poll results:
# create models here. class poll(models.model): question = models.charfield(max_length=200) pub_date = models.datetimefield('date published') numchoices = models.integerfield(default=0) def __unicode__(self): return self.question class choice(models.model): poll = models.foreignkey(poll) choice_text = models.charfield(max_length=200) votes = models.integerfield(default=0) def __unicode__(self): return self.choice_text class session_results(models.model): sessionkey=models.foreignkey(session) questionasked = models.foreignkey(poll) answerchosen=models.foreignkey(choice) def __unicode__(self): return self.sessionkey
the use case have list of questions (poll model), list of choices each choice maps question (choice model), , want keep list of sessions, can keep track of questions user has answered (session_results model). ultimately, i'd compare , stuff sessions. example if session x answered questions 1 , 2, find out other sessions answered questions 1 , 2, in mind involve self join in sql. if that's goal, should using many-to-many? thinking replacing line in session_results model:
questionasked = models.manytomanyfield(poll)
after put line in , run python manage.py sql polls, see django automatically created join table me, table doesn't seem serving future needs. read https://docs.djangoproject.com/en/1.5/topics/db/examples/many_to_many/ page, , didn't seem answer question. given want do, using raw sql queries?
thanks!!
Comments
Post a Comment