社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Prosy Arceno  »  全部回复
回复总数  1
5 年前
回复了 Prosy Arceno 创建的主题 » 在update或create django中标识更新的字段

我让它与exists()一起工作。

try:
    if MyModel.objects.filter(
        name = active_sheet["{}{}".format("A", row)].value,
    ).exists():
        # add data to list 
    else: 
        # save the new user into the user model
        MyModel.objects.create(
            name = active_sheet["{}{}".format("A", row)].value,
        )
except Exception as e:
    messages.warning(request, '%s. If you are seing this, there is an error in your file.' % e)