Reputation: 64793
trying to understand how custom admin commands work, I have my project named "mailing" and app inside named "msystem", I have written this retrieve.py to the mailing/msystem/management/commands/ folder and I have pasted an empty init.py both to the management and cpmmands folders.
from django.core.management.base import BaseCommand
from mailing.msystem.models import Alarm
class Command(BaseCommand):
help = "Displays data"
def handle(self, *args, **options):
x = Alarm.objects.all()
for i in x:
print i.name
I am weirdly getting "indention" error for handle function when I try "python manage.py retrieve" however it looks fine to me, can you suggest me what to do or point me the problem
Thanks
Upvotes: 0
Views: 582
Reputation: 43912
Your indentation needs to be consistent through the entire file, which it isn't in the snippet you posted above.
The "help = " line is indented four spaces after "class" but then the "x =" line is indented many more than four.
Maybe you are mixing spaces and tabs and thus have two tabs before "x ="?
Your code should look like this:
from django.core.management.base import BaseCommand
from mailing.msystem.models import Alarm
class Command(BaseCommand):
help = "Displays data"
def handle(self, *args, **options):
x = Alarm.objects.all()
for i in x:
print i.name
Upvotes: 4
Reputation: 10794
If you're getting an "indentation error" and everything looks aligned, this usually suggests that you're mixing tabs and spaces.
I suggest ensuring that your module is only using spaces.
Upvotes: 2