deMangler
deMangler

Reputation: 477

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. This works totally fine in one development environment but when I try it in another it fails with:

ob.objects.create(type=r[0])

AttributeError: 'Manager' object has no attribute 'objects'

As far as I can tell the virtualenvs are the same. I am using git to sync and it thinks the code is the same. 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))

Upvotes: 0

Views: 1815

Answers (1)

Lemayzeur
Lemayzeur

Reputation: 8525

this line ob = Resource.objects may be considered as the key of your bug

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

Upvotes: 1

Related Questions