У меня есть модель, которая является шаблоном для создания объектов
League(models.Model):
league = models.IntegerField(primary_key=True)
league_name =models.CharField(max_length=200)
country_code = models.ForeignKey("Country",null=True, on_delete=models.SET_NULL)
season = models.ForeignKey("Season", null=True,on_delete = models.SET_NULL, to_field = "season")
season_start = models.DateField(null = True)
season_end = models.DateField(null = True)
league_logo = models.URLField(null = True)
league_flag = models.URLField(null = True)
standings = models.IntegerField(null=True)
is_current = models.IntegerField(null=True)
cover_standings = models.BooleanField(null=True)
cover_fixtures_events = models.BooleanField(null=True)
cover_fixtures_lineups = models.BooleanField(null=True)
cover_fixtures_statistics = models.BooleanField(null=True)
cover_fixtures_players_statistics = models.BooleanField(null=True)
cover_players = models.BooleanField(null=True)
cover_topScorers = models.BooleanField(null=True)
cover_predictions = models.BooleanField(null=True)
cover_odds = models.BooleanField(null=True)
lastModified = models.DateTimeField(auto_now=True)
Данные для создания объектов из модели Лиги, поступающие из внешнего API
#get data
response = requests.get(leagues_url, headers = header)
#cutting it and paste into variables to create objects from them
leagues_json = response.json()
data_json = leagues_json["api"]["leagues"]
for item in data_json:
league_id = item["league_id"]
league_name = item["name"]
country_q =Country.objects.get(country =item["country"])
season = Season.objects.get(season = item["season"])
season_start = item["season_start"]
season_end = item["season_end"]
league_logo = item["logo"]
league_flag = item["flag"]
standings = item["standings"]
is_current = item["is_current"]
coverage_standings = item["coverage"]["standings"]
coverage_fixtures_events = item["coverage"]["fixtures"]["events"]
coverage_fixtures_lineups = item["coverage"]["fixtures"]["lineups"]
coverage_fixtures_statistics = item["coverage"]["fixtures"]["statistics"]
coverage_fixtures_players_statistics = item["coverage"]["fixtures"]["players_statistics"]
coverage_players = item["coverage"]["players"]
coverage_topScorers = item["coverage"]["topScorers"] coverage_predictions = item["coverage"]["predictions"]
coverage_odds = item["coverage"]["odds"]
#creating objects from variables
b =League.objects.update_or_create(league = league_id,league_name = league_name,country_code = country_q,season = season,season_start = season_start, etc)
Иногда в ответе, полученном по запросу к API у меня нет никаких данных, и в то время как я создаю объекты без некоторых полей, я получаю ошибки. И вот мой вопрос, как я могу проверить каждое поле, если оно имеет данные. Находится в python любым способом, кроме оператора else для решения моей проблемы