使用中间表的Django数据库设计 - 如何使用此实例?

时间:2014-02-19 15:09:46

标签: database django database-design relational-database

使用Django,我正在创建一个数据库,用于跟踪论坛中未答复的帖子以及是否/为该帖子分配了什么员工(操作员)。

模型OperatorThreadVault是永久性的,Thread是中间/临时。

我将向论坛发出api电话,每隔十分钟就会收到一份未答复的帖子列表。然后,我会检查模型thread ID中是否已存在ThreadVault。如果没有,它会将其添加到ThreadVault。然后,我将有一个临时/中间表Thread,其中包含过去10分钟未答复的帖子。每隔10分钟,表Thread将清除并刷新一批新的未答复的线程。

可能会或可能不会将操作员/员工分配给该主题。为此,我ThreadVault operator_user_name指向Operator模型。

class Operator:
    operator_ldap = models.ForeignKey(settings.AUTH_USER_MODEL,
            related_name='operator_requester')
    operator_irc_name = models.CharField(max_length="25")
    operator_user_name = models.CharField(max_length="25")

class ThreadVault:
    thread_id = models.CharField(max_length="50")
    url = models.CharField(max_length="200")
    operator_user_name = models.ForeignKey(Operator) ## Can be Empty

#intermediate table 
#Thread model clears out once every 
#10 minutes when API repopulates data
class Thread:
    url = models.ForeignKey(ThreadVault)
    author_username = models.CharField(max_length="50")
    author_name = models.CharField(max_length="50")
    thread_id = models.ForeignKey(ThreadVault)
    forum_id = models.CharField(max_length="50")
    subject = models.CharField(max_length="200")
    reply_count = models.CharField(max_length=("3"))
    latest_post_date = models.CharField(max_length=("50"))
    operator_user_name = models.ForeignKey(ThreadVault) ## Can be Empty

我知道此时我没有正确地这样做。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

这很完美:

class Operator(models.Model):
    operator_ldap = models.ForeignKey(settings.AUTH_USER_MODEL,
            related_name='operator_requester')
    operator_irc_name = models.CharField(max_length="25",
            blank=True, null=True)
    operator_user_name = models.CharField(max_length="25",
            blank=True, null=True)

class ThreadVault(models.Model):
    thread_id = models.CharField(max_length="50")
    url = models.CharField(max_length="200")
    operator_user_name = models.ForeignKey(Operator, blank=True, null=True) ## Can be Empty

#intermediate table 
#Thread model clears out once every 
#10 minutes when API repopulates data
class Thread(models.Model):
    url = models.ForeignKey(ThreadVault,
            related_name="url_vault")
    author_username = models.CharField(max_length="50")
    author_name = models.CharField(max_length="50")
    thread_id = models.ForeignKey(ThreadVault,
            related_name="thread_vault")
    forum_id = models.CharField(max_length="50")
    subject = models.CharField(max_length="200")
    reply_count = models.CharField(max_length=("3"))
    latest_post_date = models.CharField(max_length=("50"))
    operator_user_name = models.ForeignKey(ThreadVault, 
            related_name="operator_user_name_vault", 
            blank=True, null=True) ## Can be Empty