Skip to main content

django ORM Shell

---- python manage.py shell


$from django.contrib.auth.models import User
$from blog.models import Post
$user = User.objects.get(username='admin')
$Post.objects.create(

    title='One more post',
      slug='one-more-post',
      body='Post body.',
      author=user

)
>>> post.save() 

Updating objects

$post.title = 'New title' 

>>> post.save() 

Using the filter() method

$Post.objects.filter(publish__year=2015)

Post.objects.filter(publish__year=2015)\
.exclude(title__startswith='Why')

Post.objects.order_by('title')


Deleting objects

post = Post.objects.get(id=1)
post.delete()

 


 

Comments

Popular posts from this blog

Connect POSTgreSQL with Django

  First install all necessary step  $ sudo apt-get install python-pip python-dev libpq-dev postgresql postgresql-contrib Second $ sudo su - postgres $ psql Create User name with password  $ CREATE USER user_name WITH PASSWORD ' password ';   Create New Database $ CREATE DATABASE database_name WITH OWNER user_name ; Give permission  $ GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO user_name ; Update settings.py  DATABASES = { ' default ' : { ' ENGINE ' : ' django.db.backends.postgresql_psycopg2 ' , ' NAME ' : 'database_name ' , ' USER ' : ' user_name ' , ' PASSWORD ' : ' password' , ' HOST ' : ' localhost ' , ' PORT ' : ' 5432 ' , } }  Then, python3 manage.py migrate and finally create super user python3 man age.py createsuperuser