Django Tips and Tricks<br />@andymccurdy<br />http://github.com/andymccurdy/django-tips-and-tricks<br />
Something Went Wrong<br />
Debugging Production<br /># site.wsgi<br />from whiskey.core import StaffDebugWSGIHandler<br />application = handlers.Staf...
Something Useful<br />
Foreign Key Woes<br />class Profile(models.Model):<br />    name = models.CharField(max_length=64)<br />class Image(models...
Foreign Key Woes<br />class Profile(models.Model):<br />    name = models.CharField(max_length=64)<br />    icon = models....
Main Ticket: #7539<br />First Reported: 6/25/2008 !!!<br />Duplicate Tickets:<br />    #10262, #6870, #12382, #12166<br />
NullableForeignKey<br />from whiskey.core import NullableForeignKey<br />class Profile(models.Model):<br />    name = mode...
Concurrency Issues<br />def like(request, pk):<br />    m = MyObject.objects.get(pk=pk)<br />    m.likes += 1<br />    m.s...
Django "F" Expressions<br />New in Django 1.1<br />from django.db.models import F<br />def like(request, pk):<br />    m =...
.save() Updates All Fields<br />from django.db.models import F<br />def like(request, pk):<br />    m = MyObject.objects.g...
QuerySet.update()<br />from django.db.models import F<br />def like(request, pk):<br />MyObject.objects.filter(pk=pk).upda...
Even Easier<br />from whiskey.core import update<br />def like(request, pk):<br />    m = MyObject.objects.get(pk=pk)<br /...
Thanks!<br />Questions?<br />http://github.com/andymccurdy/django-tips-and-tricks<br />@andymccurdy<br />andy@whiskeymedia...
Upcoming SlideShare
Loading in...5
×

Django tips and_tricks (1)

2,304

Published on

Django Tips and Tricks

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,304
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
22
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Django tips and_tricks (1)

  1. 1. Django Tips and Tricks<br />@andymccurdy<br />http://github.com/andymccurdy/django-tips-and-tricks<br />
  2. 2. Something Went Wrong<br />
  3. 3. Debugging Production<br /># site.wsgi<br />from whiskey.core import StaffDebugWSGIHandler<br />application = handlers.StaffDebugWSGIHandler()<br />
  4. 4. Something Useful<br />
  5. 5. Foreign Key Woes<br />class Profile(models.Model):<br /> name = models.CharField(max_length=64)<br />class Image(models.Model):<br /> profile = models.ForeignKey(Profile)<br />>>> p = Profile.objects.create(name='andy')<br />>>> i = Image.objects.create(profile=p)<br />>>> p.delete()<br />>>> Image.objects.filter(profile=p)<br />[]<br />
  6. 6. Foreign Key Woes<br />class Profile(models.Model):<br /> name = models.CharField(max_length=64)<br /> icon = models.ForeignKey(Image, null=True)<br />class Image(models.Model):<br /> pass<br />>>> i = Image.objects.create()<br />>>> p = Profile.objects.create(name='andy', icon=i)<br />>>> i.delete()<br />>>> Profile.objects.filter(name='andy')<br />[] # WTF ?!?!?!?!?!?<br />
  7. 7.
  8. 8. Main Ticket: #7539<br />First Reported: 6/25/2008 !!!<br />Duplicate Tickets:<br /> #10262, #6870, #12382, #12166<br />
  9. 9. NullableForeignKey<br />from whiskey.core import NullableForeignKey<br />class Profile(models.Model):<br /> name = models.CharField(max_length=64)<br /> icon = NullableForeignKey(Image)<br />class Image(models.Model):<br /> pass<br />>>> i = Image.objects.create()<br />>>> p = Profile.objects.create(name='andy', icon=i)<br />>>> i.delete()<br />>>> Profile.objects.filter(name='andy')<br />>>> [<Profile: Profile object>]<br />>>> print Profile.objects.filter(name='andy')[0].icon<br />None<br />
  10. 10. Concurrency Issues<br />def like(request, pk):<br /> m = MyObject.objects.get(pk=pk)<br /> m.likes += 1<br /> m.save()<br /> return HttpResponse('')<br />What happens if two requests come in at the same time?<br />
  11. 11. Django "F" Expressions<br />New in Django 1.1<br />from django.db.models import F<br />def like(request, pk):<br /> m = MyObject.objects.get(pk=pk)<br /> m.likes = F('likes') + 1<br /> m.save()<br /> return HttpResponse('')<br />
  12. 12. .save() Updates All Fields<br />from django.db.models import F<br />def like(request, pk):<br /> m = MyObject.objects.get(pk=pk)<br /> m.likes = F('likes') + 1<br /> m.save() # race condition with "hates"<br /> return HttpResponse('')<br />def hate(request, pk):<br /> m = MyObject.objects.get(pk=pk)<br /> m.hates = F('hates') + 1<br /> m.save() # race condition with "likes"<br /> return HttpResponse('')<br />
  13. 13. QuerySet.update()<br />from django.db.models import F<br />def like(request, pk):<br />MyObject.objects.filter(pk=pk).update(<br /> likes=F('likes') + 1)<br /> return HttpResponse('')<br />def hate(request, pk):<br />MyObject.objects.filter(pk=pk).update(<br /> likes=F('hates') + 1)<br /> return HttpResponse('')<br />That's a lot of typing.<br />What about ORM cache invalidation?<br />
  14. 14. Even Easier<br />from whiskey.core import update<br />def like(request, pk):<br /> m = MyObject.objects.get(pk=pk)<br />update(m, likes=F('likes') + 1)<br /> return HttpResponse('')<br />def hate(request, pk):<br /> m = MyObject.objects.get(pk=pk)<br />update(m, hates=F('hates') + 1)<br /> return HttpResponse('')<br />
  15. 15. Thanks!<br />Questions?<br />http://github.com/andymccurdy/django-tips-and-tricks<br />@andymccurdy<br />andy@whiskeymedia.com<br />We're Hiring!<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×