_생강

3079 단어
= = =
@sql-models

***  
get_or_create(defaults=None, **kwargs)#      
update_or_create(defaults=None, **kwargs)#      
bulk_create(objs, batch_size=None)#      

    aggregate(args, *kwargs)
        (   ,   )
q = Blog.objects.aggregate(Count('entry'))
{'entry__count': 16}
               
q = Blog.objects.aggregate(number_of_entries=Count('entry'))
{'number_of_entries': 16}

exists()      true/false
some_queryset.exists()

update(**kwargs)              ,        
Entry.objects.filter(id=64).update(comments_on=True)

delete()       
blogs.delete()
(5, {'weblog.Blog': 1, 'weblog.Entry': 2, 'weblog.Entry_authors': 2})

         save()
entry = Entry.objects.get(pk=1)
cheese_blog = Blog.objects.get(name="Cheddar Talk")
entry.blog = cheese_blog
entry.save()#    

     add()  save()     
joe = Author.objects.create(name="Joe", tagline='Alews.')#    
entry.authors.add(joe)
entry.authors.add(john, paul, george, ringo)

***selsect  
  all()          
all_entries = Entry.objects.all()

***    filter()、exclude() get()
one_entry = Entry.objects.get(pk=1)
          ,          QuerySets
filter(**kwargs):               QuerySet
exclude(**kwargs):                 QuerySet
Entry.objects.filter(headline__startswith="What").exclude(pub_date__gte=datetime.date.today()).exclude(body_text__icontains="food")

       SQL    LIMIT OFFSET
Entry.objects.all()[5:10]    #    6   10   
      !   Entry.objects.all()[-1]     

iexact      
Blog.objects.get(name__iexact="beatles blog")
contains        
Entry.objects.get(headline__contains='Lennon')
SELECT ... WHERE headline LIKE '%Lennon%';
lt/gt     lte/gte        
Entry.objects.get(headline__lt='89')
istartswith iendswith
          
startswith endswith
           
in      
Entry.objects.filter(id__in=[1, 3, 4])
range  range  
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))
isnull     
iregex          
Entry.objects.get(title__iregex=r'^(an?|the) +')
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
day   
week       
week_day      
time      
hour      
minute    
second   
latest/earliest()      
Entry.objects.latest('pub_date')

first()/last()           
p = Article.objects.order_by('title', 'pub_date').first()

***  
Count(expression, distinct=False, **extra)
Max(expression, output_field=None, **extra)
Min(expression, output_field=None, **extra)
Sum(expression, output_field=None, **extra)


F   
   F()     、 、 、 、            
from django.db.models import F
Entry.objects.filter(rating__lt=F('n_comments') + F('n_pingbacks'))
  .bitand()、.bitor()、.bitrightshift() .bitleftshift()4    
F('somefield').bitand(16)

  SQL   %        。Django               
Entry.objects.filter(headline__contains='%')
SELECT ... WHERE headline LIKE '%\%%';

Q  django.db.models.Q,            ,           filter、exclude get   
























좋은 웹페이지 즐겨찾기