How to check is model fiel empty or not before save it to database

I have a model which is template for creating objects

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)

Data for creating objects from League model coming from external 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) 

Sometimes in response received by request to API have not some data and while I create objects without some fields I get errors. And here is my question how I can check each field is it have data. Is in python any way except if else statement to solve my problem

Upvotes: 0

Views: 16

Answers (1)

Danijel Popović
Danijel Popović

Reputation: 21

You can always set default value in your model field.

For example:

league_name = models.CharField(max_length=200, defualt='none')

Hope it helps.

Upvotes: 1

Related Questions