我寫了以下代碼: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"]添加回答
舉報
0/150
提交
取消
