简体   繁体   English

Django 2.0.3 AttributeError:'Manager'对象没有属性'objects'

[英]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.

相关问题 Django:AttributeError:“ unicode”对象没有属性“ objects” - Django : AttributeError: 'unicode' object has no attribute 'objects' AttributeError: &#39;Manager&#39; 对象没有属性 - AttributeError: 'Manager' object has no attribute Django AttributeError:“ Manager”对象没有属性“ select_for_update” - Django AttributeError: 'Manager' object has no attribute 'select_for_update' Django错误:“管理器”对象上的AttributeError没有属性“ create_user” - Django error: AttributeError at 'Manager' object has no attribute 'create_user' Django AttributeError: type object 'Rooms' 没有属性 'objects' - Django AttributeError: type object 'Rooms' has no attribute 'objects' Django 用户 Model AttributeError: 'str' object 没有属性 'objects' - Django User Model AttributeError: 'str' object has no attribute 'objects' Django:AttributeError:“对象没有属性” - Django: AttributeError: “Object has no attribute” Django:AttributeError-对象没有属性 - Django: AttributeError - Object has no attribute Kivy - AttributeError: 'DialogContent' object 没有属性 'manager' - Kivy - AttributeError: 'DialogContent' object has no attribute 'manager' Django 管理器查询在查询链中不起作用。 AttributeError: 'QuerySet' object 没有属性<the manager method></the> - Django manager queries don't work in a chain of queries. AttributeError: 'QuerySet' object has no attribute <the manager method>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM