簡體   English   中英

Django 2.0.3 AttributeError:'Manager'對象沒有屬性'objects'

[英]Django 2.0.3 AttributeError: 'Manager' object has no attribute 'objects'

我正在Django中測試一個小的管理腳本,以用模型中選擇列表中的值填充表。 這在一個開發環境中完全可以正常工作,但是當我在另一個開發環境中嘗試時,它失敗了:

ob.objects.create(類型= R [0])

AttributeError:'Manager'對象沒有屬性'objects'

據我所知,virtualenvs是相同的。 我正在使用git進行同步,它認為代碼是相同的。 有什么不同之處,這意味着它可以在一個開發環境上工作,而不能在另一個開發環境上工作?

腳本如下:

:::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))

這行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM