亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號