Django 复杂数据模型
django complex datamodel
我正在创建一个小型 Django 项目,它显示从 Twitter 数据收集的统计信息
例如我的 table 是
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')
但我不想让 userid 成为唯一原因
我想要一些东西,比如我可以手动在两个 table 中输入数据
当我在我的视图中查询时,它将搜索来自 table 的结果
例子
如果我想要@xyz 的所有趋势
或者,如果我想要所有做过#abc 趋势的用户的列表
或者如果我想要特定日期所有趋势的结果
简而言之,我希望 table 都表现得像一个
我不能使用唯一的 userid 我的日常数据大约是 20MB,所以你可以假设它很难找到 ids
我找到了我的问题的一种解决方案并且它对我有用
我只是创建没有外键或任何关系的普通 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)
我正在创建一个小型 Django 项目,它显示从 Twitter 数据收集的统计信息 例如我的 table 是
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')
但我不想让 userid 成为唯一原因
我想要一些东西,比如我可以手动在两个 table 中输入数据 当我在我的视图中查询时,它将搜索来自 table 的结果 例子 如果我想要@xyz 的所有趋势 或者,如果我想要所有做过#abc 趋势的用户的列表 或者如果我想要特定日期所有趋势的结果
简而言之,我希望 table 都表现得像一个 我不能使用唯一的 userid 我的日常数据大约是 20MB,所以你可以假设它很难找到 ids
我找到了我的问题的一种解决方案并且它对我有用 我只是创建没有外键或任何关系的普通 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)