[英]Django 2.0.3 AttributeError: 'Manager' object has no attribute 'objects'
I am testing a little management script in Django to fill a table with values from the choices list in the model. 我正在Django中测试一个小的管理脚本,以用模型中选择列表中的值填充表。 This works totally fine in one development environment but when I try it in another it fails with: 这在一个开发环境中完全可以正常工作,但是当我在另一个开发环境中尝试时,它失败了:
ob.objects.create(type=r[0]) ob.objects.create(类型= R [0])
AttributeError: 'Manager' object has no attribute 'objects' AttributeError:'Manager'对象没有属性'objects'
As far as I can tell the virtualenvs are the same. 据我所知,virtualenvs是相同的。 I am using git to sync and it thinks the code is the same. 我正在使用git进行同步,它认为代码是相同的。 What could be different that means it works on one dev environment but not another? 有什么不同之处,这意味着它可以在一个开发环境上工作,而不能在另一个开发环境上工作?
Script below: 脚本如下:
:::python
class Command(BaseCommand):
help = 'Create Initial Resources'
def add_arguments(self, parser):
pass
def handle(self, *args, **options):
self.stdout.write('Filling Resource Table')
out = ''
ob = Resource.objects
for r in Resource.Label_Choices:
if not ob.filter(type=r[0]):
ob.objects.create(type=r[0])
out = out + ":" + str(r[0])
else:
out = out + ":" + '*'
self.stdout.write(self.style.SUCCESS(out))
this line ob = Resource.objects
may be considered as the key of your bug 这行ob = Resource.objects
可被视为您的错误的关键
def handle(self, *args, **options):
self.stdout.write('Filling Resource Table')
out = ''
ob = Resource.objects.all() # Edit here
for r in Resource.Label_Choices:
if not ob.filter(type=r[0]):
Resource.objects.create(type=r[0]) # Edit here
out = out + ":" + str(r[0])
else:
out = out + ":" + '*'
self.stdout.write(self.style.SUCCESS(out))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.