为了账号安全,请及时绑定邮箱和手机立即绑定

update_or_create 导致 int() 以 10 为底的无效文字:

update_or_create 导致 int() 以 10 为底的无效文字:

一只斗牛犬 2022-04-27 16:25:00
我写了以下代码:def parse_match_data(self, match_data, statistics_data):        data = {"all_advanced_fields_populated": True}        if(match_data["match_hometeam_halftime_score"] == "" or match_data["match_hometeam_halftime_score"] == ""):            data["all_fields_populated"] = False            data["home_fh_goals"] = 0            data["home_sh_goals"] = 0            data["away_fh_goals"] = 0            data["away_sh_goals"] = 0        else:            data["all_fields_populated"] = True            data["home_sh_goals"] = 0 if int(match_data["match_hometeam_score"]) - int(match_data["match_hometeam_halftime_score"]) < 0 else int(match_data["match_hometeam_score"]) - int(match_data["match_hometeam_halftime_score"])            data["away_sh_goals"] = 0 if int(match_data["match_awayteam_score"]) - int(match_data["match_awayteam_halftime_score"]) < 0 else int(match_data["match_awayteam_score"]) - int(match_data["match_awayteam_halftime_score"])            data["home_fh_goals"] = int(match_data["match_hometeam_halftime_score"])            data["away_fh_goals"] = int(match_data["match_awayteam_halftime_score"])          required_keys = ["Ball Possession", "Goal Attempts", "Shots on Goal"]        if(statistics_data):            for statistic in statistics_data:                if(statistic["type"] in required_keys):                    data["home_" + statistic["type"].lower().replace(" ", "_")] = statistic["home"].strip('%')                    data["away_" + statistic["type"].lower().replace(" ", "_")] = statistic["away"].strip('%')使用统计方法中函数的“创建”部分似乎update_or_create一切正常,但是当它需要“更新”一个项目时,它会引发以下错误:
查看完整描述

1 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

该错误ValueError: invalid literal for int() with base 10: ''清楚地表明您正在尝试用空值 => ' ' 填充整数必填字段(可以是 FK )。所以这个字段不是必需的,可以为空,然后你必须添加blank=True, null=True到它的定义中:

your_field = models.IntegerField(default=0, blank=True, null=True)your_FK_field  = models.ForeignKey(TheOtherModel, blank=True, null=True)

或者此字段是必需的并且必须具有整数值,在这种情况下,您必须确保该值是有效的整数。

在这两种情况下,最好打印以下值以查看哪个值为空:

        home_id = fixture["match_hometeam_id"]
        away_id = fixture["match_awayteam_id"]
        league_id = fixture["league_id"]
        country_id = fixture["country_id"]
        date = fixture["match_date"] + " " + fixture["match_time"]
        home_name = fixture["match_hometeam_name"].split(" (")[0]
        away_name = fixture["match_awayteam_name"].split(" (")[0]
        league_name = fixture["league_name"]
        country_name = fixture["country_name"]


查看完整回答
反对 回复 2022-04-27
  • 1 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号