django-commands

How to filter using an expression in Django

帅比萌擦擦* 提交于 2019-12-10 10:17:53
问题 I'd like to filter that implements this "pseudocode": Post.objects.filter(Post.timestamp + Post.duration > datetime.datetime.now()) I also would like to wrap this in a Django management command. Any help would be great! 回答1: Filter Not sure how your fields look but here's a hint: Let's compose an F expression like this F('timestamp') - F('duration') , and annotate our query with it: from django.db.models import DateTimeField, ExpressionWrapper, F Post.objects.annotate( timestamp_minus

Redirection in Django command called from another command results in extraneous newlines. How can I fix this?

99封情书 提交于 2019-12-10 08:51:52
问题 The Setup For the purpose of illustrating the problem, I have created these commands in my project: foo.py : from django.core.management.base import BaseCommand from django.core.management import call_command class Command(BaseCommand): def handle(self, *args, **options): self.stdout.write("foo") # I pass `self.stdout` here explicitly because if `foo` is # redirected, I want `baz` redirected too. call_command('baz', stdout=self.stdout) baz.py : from django.core.management.base import

call_command argument is required

最后都变了- 提交于 2019-12-07 00:33:40
问题 I'm trying to use Django's call_command in a manner very similar to this question without an answer. The way I'm calling it is: args = [] kwargs = { 'solr_url': 'http://127.0.0.1:8983/solr/collection1', 'type': 'opinions', 'update': True, 'everything': True, 'do_commit': True, 'traceback': True, } call_command('cl_update_index', **kwargs) In theory, that should work, according to the docs. But it doesn't work, it just doesn't. Here's the add_arguments method for my Command class: def add

How to filter using an expression in Django

北城以北 提交于 2019-12-05 21:07:56
I'd like to filter that implements this "pseudocode": Post.objects.filter(Post.timestamp + Post.duration > datetime.datetime.now()) I also would like to wrap this in a Django management command. Any help would be great! bakkal Filter Not sure how your fields look but here's a hint: Let's compose an F expression like this F('timestamp') - F('duration') , and annotate our query with it: from django.db.models import DateTimeField, ExpressionWrapper, F Post.objects.annotate( timestamp_minus_duration=ExpressionWrapper( F('timestamp') + F('duration'), output_field=DateTimeField() ) ) Now you can

Redirection in Django command called from another command results in extraneous newlines. How can I fix this?

冷暖自知 提交于 2019-12-05 13:32:02
The Setup For the purpose of illustrating the problem, I have created these commands in my project: foo.py : from django.core.management.base import BaseCommand from django.core.management import call_command class Command(BaseCommand): def handle(self, *args, **options): self.stdout.write("foo") # I pass `self.stdout` here explicitly because if `foo` is # redirected, I want `baz` redirected too. call_command('baz', stdout=self.stdout) baz.py : from django.core.management.base import BaseCommand from django.core.management import call_command class Command(BaseCommand): def handle(self, *args,

call_command argument is required

≡放荡痞女 提交于 2019-12-05 05:38:51
I'm trying to use Django's call_command in a manner very similar to this question without an answer . The way I'm calling it is: args = [] kwargs = { 'solr_url': 'http://127.0.0.1:8983/solr/collection1', 'type': 'opinions', 'update': True, 'everything': True, 'do_commit': True, 'traceback': True, } call_command('cl_update_index', **kwargs) In theory, that should work, according to the docs . But it doesn't work, it just doesn't. Here's the add_arguments method for my Command class: def add_arguments(self, parser): parser.add_argument( '--type', type=valid_obj_type, required=True, help='Because

Can Django manage.py custom commands return a value? How, or why not?

ε祈祈猫儿з 提交于 2019-12-04 10:22:12
问题 Following the documentation: https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ I created my own custom command (called something else but example shown below): from django.core.management.base import BaseCommand, CommandError from polls.models import Poll class Command(BaseCommand): args = '<poll_id poll_id ...>' help = 'Closes the specified poll for voting' def handle(self, *args, **options): for poll_id in args: try: poll = Poll.objects.get(pk=int(poll_id)) except Poll

How to make db dumpfile in django

天大地大妈咪最大 提交于 2019-12-03 15:14:36
问题 I want to make a dump in django irrespective of database I am using and can be loaded later. The command 'dumpdata' is perfect for this, but it is printing output on console. More over I am calling it using call_command function so I cannot store its content in any variable as it is printing output on console. Please let me know how store dump to a file using dumpdata or any other command or api. Thanks 回答1: You can choose a file to put the output of dumpdata into if you call it from within

How to make db dumpfile in django

感情迁移 提交于 2019-12-03 04:55:17
I want to make a dump in django irrespective of database I am using and can be loaded later. The command 'dumpdata' is perfect for this, but it is printing output on console. More over I am calling it using call_command function so I cannot store its content in any variable as it is printing output on console. Please let me know how store dump to a file using dumpdata or any other command or api. Thanks You can choose a file to put the output of dumpdata into if you call it from within Python using call_command , for example: from django.core.management import call_command output = open(output

Why is run called twice in the Django dev server?

余生长醉 提交于 2019-11-27 22:21:34
I want to make the Django development server do something before it starts running. To do this, I created a new app, added it to the top of INSTALLED_APPS , and then created a management/commands/runserver.py file in the app with the following code: from django.contrib.staticfiles.management.commands.runserver import Command as RunserverCommand class Command(RunserverCommand): def run(self, *args, **options): self.stdout.write('About to start running on ' + self.addr) super(Command, self).run(*args, **options) (The thing I actually want to do is more complicated than writing one line to stdout