我是Python和Django的新手,我遇到了问题,我无法找到答案。 我将Django 1.7与SQLite3和Python 3.4结合使用。 我试图通过从第三方网站获取数据并将其发布到我自己的网站来制作体育联赛表。 到目前为止,我可以从网上获取所需的数据。我已经将表设置为我的SQLite数据库,并且可以使用django-tables2 app将我手动输入的数据写入我的页面。 问题是,我无法弄清楚,如何自动化我的脚本,以便它可以自动将数据写入SQLite中的表。
这是我的models.py文件:
from django.db import models
import django_tables2 as tables
from django.db import connection
class Ranking(models.Model):
Team_name = models.CharField(max_length=25,unique=True)
League = models.CharField(max_length=15,)
League_points = models.CharField(max_length=3,)
# If I just add the rows from def te() to the end of the script and run the
#server with manage.py runserver, it adds values "Team name", "First league",
#"11" to the correct places, but closes the server instantly, because these
#values have been added already.
def te():
cursor = connection.cursor()
cursor.execute("""INSERT INTO EEtabel_ranking ('Team_name', 'League', 'League_points') VALUES (?, ?, ?)""", ("Team name", "First league", "11"))
def __str__(self):
return ' '.join([
self.Summoner_name,
self.League,
self.League_points,
])
# This is for creating the table with data from database
class SimpleTable(tables.Table):
class Meta:
model = Ranking
现在,我想要实现的目标是将数据提供给表格并每天更新一次。因此,如果访问者访问我的页面,他们会看到最新的更新表,并且不会触发新的更新。更新应该在固定的时间发生。
这是一个脚本,可以帮助我从网上获取数据:
from wsgi.openshift.myapp import MyAppClient # MyApp is an app, which connects to the website, I'm making a connection to, to get data from there.
list_of_team_names = ['team1', 'team2', 'team3', 'etc']
client = MyAppClient.MyAppClient(key, "league_name")
team = client.search(list_of_team_names)
for x in list_of_team_names:
print(team.name)
league = client.league_data()
print(league.tier)
print(league.entries.leaguePoints)
team = client.next()
#What this does, is that it searches the results of the teams in the "list_of_team_names".
#And gets the results one by one.
结果如何(示例): TEAM1 第一联赛 34 TEAM2 第一联赛 45 Team3 第一联赛 10 等
总结这个可怕的长篇故事 - 我的问题简短:
1)如何正确地将数据传送到SQLite数据库?
2)如何使数据被覆盖?
我的意思是,如果我已经在桌上有34分的Team1。在收到3分后,我的桌子看起来不像:Team1 34分,Team1 37分。它应该取代以前的记录。
3)如何让数据每天更新一次(或每天一小时或两次(自定义时间))?
我提前感谢你,并希望有一天我会有更多的经验,所以我不会再问这些愚蠢的问题了。 :P
答案 0 :(得分:4)
像Django这样的平台提供Object-relation mappers,允许您抽象出与数据库的直接交互,并改为使用编程对象。
说实话,我不知道你是否正确地编写了数据库查询,而且是粗暴的,我不在乎。 Django-ORM存在的原因非常充分,它使事情变得更容易。您是否可以制作原始数据库查询以将行插入到django数据库中,但这更容易:
from wsgi.openshift.myapp import MyAppClient
list_of_team_names = ['team1', 'team2', 'team3', 'etc']
client = MyAppClient.MyAppClient(key, "league_name")
team = client.search(list_of_team_names)
for x in list_of_team_names:
ranking = Ranking(
Team_name = team.name
League = league.tier
League_points = league.entries.leaguePoints
)
ranking.save()
models.py
用于定义您要使用的对象,因此通过实例化一个对象,您可以为数据库做好准备(这就是为什么在这种情况下需要显式调用save)。
我们可以改为创建和保存,如下所示:
ranking = Ranking.objects.create(
Team_name = team.name
League = league.tier
League_points = league.entries.leaguePoints
)
或者,直接获取一个对象,或者如果不存在则创建:
was_created, ranking = Ranking.objects.get_create(
Team_name = team.name
League = league.tier
League_points = league.entries.leaguePoints
)
这可能比检查对象是否存在并返回它们所需的相关SQL查询和代码更短更清晰。