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

Data visualize with matplotlib(solid line)

Nominal GDP      from matplotlib import pyplot as plt      years = [1950, 1960, 1970, 1980, 1990, 2000, 2010]      gdp = [300.2, 543.3, 1075.9, 2862.5, 5979.6, 10289.7, 14958.3]      plt.title('GDP')      plt.ylabel('Billios of $')      plt.plot(years,gdp,color='red',marker='o',linestyle='solid')      plt.show() * plt.plot(x,y,color,marker per year,linestyle) there are several line style '-', '--', '-.', ':', 'None', ' ', '', 'solid', 'dashed', 'dashdot', 'dotted' fig-