我有一个模型,该模型是用于创建对象的模板
byte[] bb;
Bitmap bmp = this.screenshots.RemoveFromFront();
Stream stream = new MemoryStream();
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
bmp.Save(stream, GetEncoder(ImageFormat.Jpeg), encoderParameters);
using (var compressStream = new MemoryStream())
using (var compressor = new DeflateStream(compressStream, CompressionMode.Compress))
{
stream.CopyTo(compressor);
compressor.Close();
bb = compressStream.ToArray();
}
用于通过联赛模型从外部API创建对象的数据
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的请求收到的响应中没有一些数据,而当我创建没有某些字段的对象时,会出错。这是我的问题,如何检查每个字段是否有数据。在python中以任何方式显示,除非else语句可以解决我的问题
答案 0 :(得分:1)
您始终可以在模型字段中设置默认值。
例如:
league_name = models.CharField(max_length=200, defualt='none')
希望有帮助。