我需要检查是否已创建其他模型,并填充字段。 如果另一个模型具有任何值的字段,则尝试创建的当前模型不应该发生。如果可能的话发送一条错误消息。Django - 在保存模型前检查一些字段
这是我当前的代码:
class Video(models.Model):
#####
# Fields of model
#####
def save(self, force_insert=False, force_update=False, *args, **kwargs):
some_video = Video.objects.all().filter(field_boolean=True).first()
if not some_video:
# Save current model
super(Video, self).save(force_insert, force_update, *args, **kwargs)
else:
# avoid save method for the current model created and send error message
什么我做错了,否则我缺少的是什么?什么是正确的方法来做到这一点?
什么问题? –
@SergeyGornostaev米码不起作用。 – Dvex