2012-03-18 35 views
2

2つのセキュリティに関する質問とそれに対応する回答が必要です。セキュリティの質問は私によって提供されるので、<select>のようなものになります。私はUserProfileという名前のユーザープロファイルを格納するモデルを作成しました。私のような何かを行うことができ私のDjangoサイトにセキュリティの質問を追加する

class UserProfile(models.Model): 
    phone1 = models.CharField(help_text='Primary phone number') 
    phone2 = models.CharField(help_text='Secondary phone number', blank=True) 
    ... 

SECURITY_QUESTIONS_CHOICES = (
    ('PN', 'What is your telephone number?'), 
    ('BF', 'What is the full name of your best friend?'), 
    ... 
    ) 

をし、私のモデルに次の二つのフィールドを追加します。それは次のようになります

question1 = models.CharField(choices=SECURITY_QUESTIONS_CHOICES) 
question2 = models.CharField(choices=SECURITY_QUESTIONS_CHOICES) 

が、私は変更することができるようにしたいですセキュリティの質問のリスト、私はそれもモデルにしたい。

私の質問は:

同じモデルを指す2つのフィールドを持つことの最善の方法は何ですか?

  • のみ1フィールド(例えば、questionsSecurityQuestionに多対多の関係である有し、登録フォームでに数を制限しますか?
  • 2つのフィールド(question1およびquestion2)があり、それぞれがForeignKeyからSecurityQuestionですか?

答えて

4

すべてのセキュリティに関する質問に対して、別のモデルを作成することをお勧めします。柔軟性を提供します。

class SecurityQuestions(models.Model): 
    class Meta: 
     db_table = 'security_questions' 
    id = models.AutoField(primary_key=True) 
    question = models.CharField(max_length = 250, null=False) 

class UserProfile(models.Model): 
    ---- 
    ---- 
    user_questions = models.ManyToManyField(SecurityQuestions, through='SecurityQuestionsInter') 


class SecurityQuestionsInter(models.Model): 
    class Meta: 
     db_table = 'security_questions_inter' 

    profile = models.ForeignKey(Profile) 
    security_questions = models.ForeignKey(SecurityQuestions) 
    answer = models.CharField(max_length = 250, null=False) 
+0

いい考えです。私は、(フォームを通じて)ユーザーが常に正確に2つの質問を持つことを確認する必要があります。 –

+1

これらの回答は、パスワードのように安全なハッシュに保存する必要があります。なぜなら、データベースが侵害された場合、誰かのアカウントに同じアクセス権を与えるからです。 –

+0

@TimSaylorはいあなたは絶対に正しいです。ありがとう –

関連する問題