Introduction
Writing one liners are cool. Let's try some.
Installations and configuration
pip install devtools==0.7.0
pip install -U django==3.2.*
mkdir myproject && cd myproject && django-admin startproject config .
Also, you did the migration
python manage.py migrate
Hello world
python -c "print('hello world')"
Another example is as follows,
python -c "from datetime import datetime; print(datetime.now())"
# Now lets take looping example,
python -c "for i in range(10): print(f'{i} x {7} = {(i+1)*7}')"
# Now let's write this to a file
python -c "with open('myfile.txt','w') as myfile: myfile.write('Hello world')"
myfile.txt
is created in same folder you run above command.
Django one liners
Print Timezone Now time with Django's default timezone
python manage.py shell -c "from django.utils import timezone; print(timezone.now())"
Create a new admin user with username and password
python manage.py shell -c "from django.contrib.auth import get_user_model; User=get_user_model(); a=User.objects.create(username='a'); a.set_password('a'); a.is_superuser=True; a.is_staff=True; a.save()"
Print a query list values
python manage.py shell -c "from django.contrib.auth import get_user_model; User=get_user_model(); print(User.objects.values_list())"
Print user count
python manage.py shell -c "from django.contrib.auth import get_user_model; User=get_user_model(); print(User.objects.count())"
Tested on Debian 11, Python 3.6
Conclusion
One-liners are awesome. By using these one-liners. You can save some time.
Top comments (0)