Forums

Django 2.0.3 - ob.objects.create() works on dev but not on PA

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 my development environment but when I try it in Python Anywhere it fails with:

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

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

As far as I can tell the virtualenvs at PA and my dev are the same. I am using git to sync and it thinks the code is the same.

Script below:

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

Hmm, I'm not sure why it would work differently locally and on PythonAnywhere, but the code doesn't look quite right to me -- you have:

ob = Resource.objects

...and then later

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

...which means that in effect you have

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

-- note the doubled ".objects". I'd expect the second line to be simply:

ob.create(type=r[0])

You are absolutely correct. This should not work it is a typo. The strange thing it that it does work on my dev environment. I corrected it and now it works both in PA and my dev box. I posted on StackOverflow to see if anyone can throw any light on why this would work under any circumstances so we shall see. What usually happens is my questions get down-voted and eventually I delete them but you never know someone else may have had this strangeness.

Yes, it's definitely odd that it worked at all on your dev box. If you do get an answer on Stack Overflow, we'd love to know what it is!

Hehe.... was bound to be something simple. I did get an answer on Stack Overflow. There must have been some difference in what was in the database (the db's are excluded from git) so on my box it was not getting to the faulty line because if not ob.filter(type=r[0]): was never true, but it must have been true on PA. Obviously not got my debugging head on today. Good to have a forum to shame myself into being more careful :)

D'oh! I should have realised that would explain the difference too. Consider us both jointly shamed on that one ;-)