1

is it possible to have on the same admin command class option to run it with or without args.

like having if args == "" (run this command) else: (run this)?

Thanks in advance.

1 Answer 1

3

Sure, just check 'args' as passed to the handle method like this:

from django.core.management.base import BaseCommand

class Command(BaseCommand):
    help = 'prints no args if there are no args'

    def handle(self, *args, **options):
        if len(args) == 0:
            print 'no args'
        else:
            for pkid in args:
                print pkid
Sign up to request clarification or add additional context in comments.

2 Comments

What I am trying to do is.... if I put some number as args (in my case it will be pk value (digit)) than I will run a code that will bring specific pk id. and if my args are empty I will run a different code to bring all id's from db.
@yaniv14 - I updated my answer to check for no args rather than a specific argument. I think the updated code should be closer to what you are trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.