python - django:避免在 get_or_create 上出现 "Unused variable"警告

标签 python django

有没有办法避免在 get_or_create 方法上出现“未使用的变量”警告?

level,created= Level.objects.get_or_create( name='Alumnes x Classificar')

因为我不需要读取 created 变量,所以 IDE 对此显示警告。

我知道我使用变量的一个简单解决方案,比如:if create: pass 但我正在寻找更优雅的解决方案。

再见!

最佳答案

这样做的一个“标准”方法是使用 _ 变量:

level, _ = Level.objects.get_or_create(…)

_ 是一个“真正的变量”(只要您可以读取和写入它),但它通常被理解为“忽略”。

关于python - django:避免在 get_or_create 上出现 "Unused variable"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6998586/

相关文章:

javascript - Django - 在模板中显示 Json 或 Httpresponse

python - Python 中的 FFT 及其解释

python - 如何计算运行特定 Python 程序需要多少 CPU、RAM 和能量?

django - 无法访问静态资源

python - Django 通用关系不起作用

python - 在 Python 中获取相对路径的任何优雅方法?

python - 仅使用 keras 对 float 数组进行 one-hot 编码

python - Django、Python 3.4、CentOS 6.4、虚拟环境 : ImportError: No module named Django

python - Django ORM for select max(field1) from table group by (field2);

django - 在 Django 中的表单错误消息中使用模板标签