I'm new to Django, I have created a selenium project in which It automates mobile recharge. After completion of "recharge successful" I need it to send a successful receipt into my database. I used print(order_id.text) to get receipt in my terminal. now I don't know how to send that receipt to my database.
Probably the easiest way to implement this is with a custom management command [Django-doc]. In your amazon app, yoou can define a management command:
amazon/
management/
commands/
amazonpay.py
# …
In that amazonpay.py file, you then implement the custom management command:
# amazon/management/commands.amazonpay.py
from django.core.management.base import BaseCommand
from amazon.models import Amazon
class Command(BaseCommand):
help = 'Some description...'
def handle(self, *args, **options):
# … run selenium …
Amazon.objects.create(
o=ord_id
)
You can then run this command with:
python3 manage.py amazonpay
In fact runserver, makemigrations, etc. are all defined as management commands as well.
Related
I am having issues updating a website I run. Essentially, the users on the website cannot be used anymore, so people have to create new accounts. However, users have ManyToMany relations with other tables in the database, and I want to be able to move the relations from the old users to the new ones.
Is there any good way to do this in Django or directly in the database? I am using PostgreSQL.
Django has a management command framework for this.
https://docs.djangoproject.com/en/3.1/howto/custom-management-commands/
This should live in PROJECT/APPNAME/management/commands/
from django.core.management import BaseCommand
from django.contrib.auth import get_user_model
User = get_user_model()
#The class must be named Command, and subclass BaseCommand
class Command(BaseCommand):
# Show this when the user types help
help = "fix users relations"
# A command must define handle()
def handle(self, *args, **options):
self.stdout.write("fixing users begin")
for user in User.objects.all():
...user fix code...
self.stdout.write("fix users complete")
I have a very simple case in which I want to run a script with python
myscript.py:
from models import Company
c = Company(number="1234567890", name="Company name")
models.py:
from django.db import models
class Company(models.Model):
number = models.CharField("company number", max_length=20, unique=True)
name = models.CharField("company name", max_length=30, unique=True)
class Meta:
verbose_name = "Company"
verbose_name_plural = "Companies"
I want to run python myscript.py or something like python manage.py execute_file myscript.py
I know my question is trivial and I can import a python manage.py shell environment, but that's not the point. Can I run it in a much simple manner?
Yes, Django has support for that: you define a custom command. It has some support for example to parse command parameters in a more convenient way.
You first need to define a file with the same name as the command you want (so here myscript.py). This should be placed in the app/management/commands/ directory (where app is an app of your system). So then the file tree looks like:
app/
__init__.py
management/
__init__.py
commands/
__init__.py
myscript.py
If you have not constructed any custom management commands before, you probably need to construct the files in boldface. The __init__.py files are simply empty files.
Then your command can look like:
# app/management/commands/myscript.py
from django.core.management.base import BaseCommand
from models import Company
class Command(BaseCommand):
def handle(self, *args, **options):
c = Company(number="1234567890", name="Company name")
Note that this script is actually not doing much: you construct a Company, but you do not store it in the database, so that means that usually the program will just stop, or raise an error in case the input is invalid.
You can then call your script with:
python3 manage.py myscript
and the command will also be listed in case you ask for help (with python3 manage.py help).
The Django tutorial on custom commands explains in more depth what you can do to parse commands, add helptext, etc.
from django.core.management.base import BaseCommand
def Command(BaseCommand):
def handle(self, *args, **options):
self.stdout.write( "lol" )
So I tried creating a custom command in Django 1.8. It is in the directory appname/management/commands/commandname.py . However, trying to run the command using:
python manage.py commandname
yielded this error:
TypeError: Command() takes exactly 1 argument (0 given)
I've ensured that all the directories contain an __ init__.py file and that the app is added to the project. There doesn't seem to by much info on this online. Please do help.
Based on this doc page
(https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/), you may want to change that line from def Command(BaseCommand): to class Command(BaseCommand):. (it looks like some further changes will be necessary as well)
I'm try to Execute Django shell command as cron,
I have some queries and objects tasks to search and read and write using the models and queries of my django app.
How can I execute this 1 or 2 times a day?
For example, how can I run these queries periodically:
from django.contrib.auth.models import User
from perfil.models import *
for user in usuarios:
profiles = Perfil.objects.filter(user=user)
create_task = Task.objects.create(user=user)
Take a look at Custom management commands for django.
As a basic example:
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User
from perfil.models import *
class Command(BaseCommand):
help = 'What does this do? '
def handle(self, *args, **options):
for user in usuarios:
profiles = Perfil.objects.filter(user=user)
create_task = Task.objects.create(user=user)
On a side note, you should be more explicit about your import and not use from perfil.models import *.
From that, you can execute the command based on the file you saved it in. If you saved the file in yourapp/management/commands/dofunstuff.py than you could execute it via python manage.py dofunstuff.
I've had some trouble trying to reset my cache every hour for a particular django view.
Right now, I am using the cache_page decorator to cache my view using Memcached. But the cache expires after a while and the request is uncached from some users.
#cache_page(3600)
def my_view(request):
...
How do I write my own django manage.py command to refresh my cache for this view every hour from cron?
In other words, what do I put in my refresh_cache.py file mentioned in the answer here:
Django caching - can it be done pre-emptively?
Thanks!
In your app, you can create a folder called management which contains another folder commands and an empty __init__.py file. Inside commands you create another __init__.py and a file where you write your custom command. Let's called it refresh.py:
# refresh.py
from django.core.management.base import BaseCommand, CommandError
from main.models import * # You may want to import your models in order to use
# them in your cron job.
class Command(BaseCommand):
help = 'Posts popular threads'
def handle(self, *args, **options):
# Code to refresh cache
Now you can add this file to your cron jobs. You can take a look at this tutorial but basically you use crontab -e to edit your cron jobs and crontab -l to see which cron jobs are running.
You can find all of this and more in the Django documentation.
I want to expand on Roberts answer to fill out the # Code to refresh cache
Timezome+location make caches much hard to work with, in my case I just disabled them, also I am not sure about using the test methods in the application code, but it seems to work nicely.
from django.core.management.base import BaseCommand, CommandError
from django.test.client import RequestFactory
from django.conf import settings
from ladder.models import Season
from ladder.views import season as season_view
class Command(BaseCommand):
help = 'Refreshes cached pages'
def handle(self, *args, **options):
"""
Script that calls all season pages to refresh the cache on them if it has expired.
Any time/date settings create postfixes on the caching key, for now the solution is to disable them.
"""
if settings.USE_I18N:
raise CommandError('USE_I18N in settings must be disabled.')
if settings.USE_L10N:
raise CommandError('USE_L10N in settings must be disabled.')
if settings.USE_TZ:
raise CommandError('USE_TZ in settings must be disabled.')
self.factory = RequestFactory()
seasons = Season.objects.all()
for season in seasons:
path = '/' + season.end_date.year.__str__() + '/round/' + season.season_round.__str__() + '/'
# use the request factory to generate the correct url for the cache hash
request = self.factory.get(path)
season_view(request, season.end_date.year, season.season_round)
self.stdout.write('Successfully refreshed: %s' % path)