annotate django queryset code example

Example 1: django pandas queryset

import pandas as pd
import datetime
from myapp.models import BlogPost

df = pd.DataFrame(list(BlogPost.objects.all().values()))
df = pd.DataFrame(list(BlogPost.objects.filter(date__gte=datetime.datetime(2012, 5, 1)).values()))

# limit which fields
df = pd.DataFrame(list(BlogPost.objects.all().values('author', 'date', 'slug')))

Example 2: objects.filter django

>>> Entry.objects.filter(blog_id=4)

Example 3: django order by

#Add this to your models.py class, and dont remove the comma!
class Meta:
        ordering = ('yourfeild',)

Example 4: django filter exsist

if table.objects.filter(prop='value').count() > 0:

Example 5: django queryset count

# Returns the total number of entries in the database.
Entry.objects.count()

# Returns the number of entries whose headline contains 'Lennon'
Entry.objects.filter(headline__contains='Lennon').count()