在for循环中创建对象 [英] Create objects in for loop

查看:70
本文介绍了在for循环中创建对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建十个 User 对象?

for i in range(11):
    i = User.objects.create(username = 'test125', email='test@mail.com', password='pass1')

用户名列不是唯一

推荐答案

您在 username 字段上有一个唯一的约束,这意味着您不能有两个对象具有相同的 username .试试这个:

You have a unique contraint on the username field which means you cannot have two objects with the same username. Try this:

for i in range(10):
    i = User.objects.create(username = 'testX%s' % i, email='test@mail.com', password='pass1')

这篇关于在for循环中创建对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆