首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向我的Django站点添加安全问题

向我的Django站点添加安全问题
EN

Stack Overflow用户
提问于 2012-03-18 23:00:53
回答 1查看 1.3K关注 0票数 2

我需要向用户询问的两个安全问题及其相应的答案。安全问题将由我提供,所以它将是某种<select>。我创建了一个模型来存储名为UserProfile的用户配置文件。看上去:

代码语言:javascript
复制
class UserProfile(models.Model):
    phone1 = models.CharField(help_text='Primary phone number')
    phone2 = models.CharField(help_text='Secondary phone number', blank=True)
    ...

我可以做这样的事情:

代码语言:javascript
复制
SECURITY_QUESTIONS_CHOICES = (
    ('PN', 'What is your telephone number?'),
    ('BF', 'What is the full name of your best friend?'),
    ...
    )

并将以下两个字段添加到我的模型中:

代码语言:javascript
复制
question1 = models.CharField(choices=SECURITY_QUESTIONS_CHOICES)
question2 = models.CharField(choices=SECURITY_QUESTIONS_CHOICES)

但我希望能够修改安全问题的列表,所以我希望它也是一个模型。

我的问题是:

,有两个指向相同模型的字段的最佳方法是什么?

  • 只有一个字段(如。questions)是ManyToMany与SecurityQuestion的关系,并将注册表格中的数字限制为2
  • 有两个字段(question1question2),其中每个字段都是ForeignKey to SecurityQuestion
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-18 23:51:53

我更希望为所有的安全问题创建一个单独的模型。它给了你灵活性。

代码语言:javascript
复制
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)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9763099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档