Implementing a custom management command

As I was required to implement a custom management command in my project, I tried to create a dummy command as part of learning. When I executed the custom management command I created, I got the result twice…

My folder structure looks like this

training/
    __init__.py
    management/
        __init__.py
        commands/
            __init__.py
            custom_management.py

Here’s the custom management command I’ve created


from django.core.management.base import BaseCommand


class Command(BaseCommand):
    """
    Creates a custom command for updating the previous assessment records
    """

    help = "Updates the assessments recordes in a SubBatch"

    def add_arguments(self, parser):
        """
        This function is responsible for adding arguments to the command
        """
        parser.add_argument("--list_a", nargs="*", type=int)
        parser.add_argument("--int_x", type=int, default=0)

    def handle(self, *args, **kwargs):
        """
        Create records based on the given scenario
        """
        x = sum(kwargs["list_a"])
        y = kwargs["int_x"]
        add = x + y
        return self.stdout.write(
                self.style.SUCCESS('Sum of the values "%i"' % add)
            )

When I tried to execute the command, this is the result I got

Can someone explain to me why the result is returned twice?
Also when I try to run my testcases, it gets executed twice…
Thanks in advance

I’ve copy pasted your code and run in my project and it works fine in my existing project as you can see in below screenshot.

In this below folder structure base refers to my app name.

base
├── __init__.py
├── management
│   ├── commands
│   │   ├── custom_management.py
│   │   ├── __init__.py
│   ├── __init__.py

Also I removed return from here

        return self.stdout.write(
            self.style.SUCCESS('Sum of the values "%i"' % add)
        )

and used this and still works

        self.stdout.write(
            self.style.SUCCESS('Sum of the values "%i"' % add)
        )

Yeah, this will not raise any issue in normal circumstances. But somehow I’m facing this issue in my project am I missing something in my project?
This is not the only issue, when I execute my test cases, it gets executed twice
Similarly for makemigrations and migrate commands also

That looks to me like you’ve either got a badly-modified manage.py command or a user-written python script that is running things twice.

I also see where you’re apparently not using a virtual environment - is this work being done in a docker container?

Thanks for the help, I found the issue. Yes, I’ve previously modified the manage.py file and changed it back to normal.

Yes, this has been executed in the docker container…
I use conda for creating an environment.