向我的Django网站添加安全性问题 [英] Adding security questions to my Django site
本文介绍了向我的Django网站添加安全性问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要向用户询问两个安全问题及其相应的答案。安全问题将由我提供,因此将是某种< select>
。我创建了一个模型来存储名为 UserProfile
的用户配置文件。看起来像这样:
I need to ask the user for two security questions and their corresponding answers. The security questions will be provided by me, so it will be some kind of <select>
. I created a model to store the user profile named UserProfile
. It looks like:
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?'),
...
)
并将以下两个字段添加到我的模型中:
and add the following two fields to my model:
question1 = models.CharField(choices=SECURITY_QUESTIONS_CHOICES)
question2 = models.CharField(choices=SECURITY_QUESTIONS_CHOICES)
但我希望能够修改安全问题列表,所以我也希望它也可以成为一个模型。
but I want to be able to modify the list of security questions, so I want it to be a model too.
我的问题是:
具有两个指向同一模型的字段的最佳方法是什么?
- 只有一个字段(例如
问题
),该字段与ManyToMany关系SecurityQuestion
,并在注册表格中将数字限制为 2 ? - 具有两个字段(
question1
和question2
),其中每个字段都是一个ForeignKey
到SecurityQuestion
?
- Having only one field (eg.
questions
) which is a ManyToMany relationship toSecurityQuestion
, and restricting the number to 2 in the registration form? - Having two fields (
question1
andquestion2
) where each one is aForeignKey
toSecurityQuestion
?
推荐答案
我希望为所有安全问题创建一个单独的模型。
I would prefer create a separate model for all security questions. It gives you flexibility.
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)
这篇关于向我的Django网站添加安全性问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文