【问题标题】:django complex datamodeldjango 复杂数据模型
【发布时间】:2016-09-26 02:08:27
【问题描述】:

我正在创建一个小型 Django 项目,该项目显示从 twitter 数据收集的统计信息 例如我的桌子是

hashDetails
---------------------------------------------
id   hashname tweetPosted  trendDate  userid
---------------------------------------------
1    #abc       44        2-2-2016    @xyz
2    #abc       55        2-2-2016    @qwer
3    #xcs       55        3-2-2016    @qwer
4    #xcs       55        4-2-2016    @qwer
---------------------------------------------

userDetails
----------------------------------------------
id    userid     profileImage   profileImage
----------------------------------------------
1     @xyz        image2.jpg     www.abc.com
2     @qwer       image3.jpg     www.xadf.com
----------------------------------------------

为此,如果我创建 models.py

class userDetails(models.Model):
    userid= models.CharField(max_length=30)
    profileImage= models.CharField(max_length=30)
    profileImage= models.CharField(max_length=30)

class hashDetails(models.Model):
    hashname= models.CharField(max_length=30)
    tweetPosted= models.IntegerField()
    trendDate= models.DateTimeField()
    userid = models.ForeignKey(userDetails, to_field ='userid')

但我不想让用户 ID 独一无二

我想要一些东西,比如我可以在两个表中手动输入数据 当我在我的视图中查询时,它将从两个表中搜索结果 例子 如果我想要@xyz 的所有趋势 或者如果我想要列出所有做过#abc 趋势的用户 或者如果我想要特定日期所有趋势的结果

简而言之,我希望两个表都表现得像一个 我不能将用户 ID 用作唯一用户,我的每日数据大约为 20MB,因此您可以假设它很难找到 ID

【问题讨论】:

    标签: sql django database relational-database foreign-key-relationship


    【解决方案1】:

    我找到了我的问题的一种解决方案并且它对我有用 我只是创建没有外键或任何关系的普通 2 模型 并在views.py中定义我的功能 得到了我想要的结果

    def teamdetail(request,test_id):
        hashd = hashDetails.objects.get(hashname=test_id)
        userd=  userDetails.objects.all()
        context = {'hashinfo': hashd, 'username':userd}
        return render(request,'test/hashDetails.html',context)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 2013-06-03
      相关资源
      最近更新 更多