开发者

django: avoid "Unused variable" warning on get_or_create

开发者 https://www.devze.com 2023-03-26 19:52 出处:网络
Is there a way to avoid \"Unused variable\" warning on get_or_create method? level,created= Level.objects.get_or_create( name=\'Alumnes x Classificar\')

Is there a way to avoid "Unused variable" warning on get_or_create method?

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

because I don't need to read created variable, IDE show a 开发者_JAVA百科warning about this.

I know that a trivial solutions i to use variable, some thing like: if create: pass But I'm looking for a more elegant solution.

See you!


One “standard” way of doing this is to use the _ variable:

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

_ is a “real variable” (insomuch as you can read from and write to it), but it's generally understood to mean “ignored”.


You could only capture the level by doing:

level = Level.objects.get_or_create( name='Alumnes x Classificar')[0]
0

精彩评论

暂无评论...
验证码 换一张
取 消