Meetup django common_problems(1)
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Meetup django common_problems(1)

  • 2,980 views
Uploaded on

Slides from 5/11/2011 Milwaukee Django User Group Meetup

Slides from 5/11/2011 Milwaukee Django User Group Meetup

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,980
On Slideshare
2,980
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
39
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1.
  • 2. Who is this fool?!
    A little about me
  • 3. Graphic Art
    Photography
    Web Design
    Django
    VFX
    JavaScript
    Print Design
    Software
    Digital Media
    CSS
    Python
    Flash / Flex
  • 4.
  • 5. PERL & JavaScript
  • 6.
  • 7. Django = HotPileOfAwesome( yes=True )
    Django.build_web_app( fast=True )
  • 8. Django.make_an_app()
    >>> True
    Django.log_in_user()
    >>> True
    Django.comment_on_my_model()
    >>> True
    Django.message_my_user(user='myuser')
    >>> True
    Django.send_emails( emails=['me@mail.com'] )
    >>> True
    Django.do_complex_SQL( please=True )
    >>> No Problem!
  • 9. Django.make_a_thumbnail()
    >>> Thumbnail?
    Django.send_text_message()
    >>> Email Exception: no such thing
    Django.search_all_my_stuff()
    >>> WTF?
    Django.get_data_in_under_75_queries()
    >>> Whoa...
    Django.alter_table(model='MyModel')
    >>> Let's not get crazy
    Django.be_restful( now=True )
    >>> you meanrequest.POST
  • 10. I've Got an
    app for that!
  • 11. Searching
  • 12. Searching
    Find Stuff - Fast
  • 13. Searching
    Find Stuff - Fast
    ( without crushing my DB )
  • 14. Haystack
    haystacksearch.org
    Djapian
    code.google.com/p/djapian/
    Sphinx
    django-sphinx ( github )
  • 15. Django
    MyModel.objects.filter( text__icontains='word' )OR MyModel.objects.filter( text__search='word' )
    Problems:
  • Haystack
    classPostIndex( SearchIndex ):
    body = CharField(document = True, model_attr = 'body')
    title = CharField( model_attr = 'title')
    author = CharField( model_attr = 'author__get_full_name')
    text = CharField( use_template = True )
    defget_queryset( self ):
    return Post.objects.all()
    defprepare_url( self, obj ):
    return obj.get_absolute_url()
    site.register(Post, PostIndex)
  • 19. Haystack
    SearchQuerySet()
    .filter(
    SQ(field=True)
    | SQ(field__relation="something")
    ~SQ(field=False)
    )
    >>> [ <SearchResult>, <SearchResult>, <SearchResult> ]
  • 20. Xapian
    classArticleIndexer( Indexer ):
    fields = ['title','body']
    tags = [
    ('title','title', 3),
    ('body', 'as_plain_text', 1)
    ]
    space.add_index(Article, ArticleIndexer, attach_as='indexer')
  • 21. Xapian
    fromdjapian.indexer import CompositeIndexer
    flags = xapian.QueryParser.FLAG_PARTIAL|
    xapian.QueryParser.FLAG_WILDCARD
    indexers = [ Model_1.indexer, Model_2.indexer ]
    comp = CompositeIndexer( *indexers )
    s = comp.search( `a phrase` ).flags( flags )
    >>> [ <hit:score=100>,<hit:score=98> ]
    $ s[0].instance
    >>> <ModelInstance:Model>
  • 22. Haystack
    Xapian
    Index files
    Class Based Index
    Customize Text For Indexing
    Link to Indexed Object
    Index Fields, Methods & Relations
    Stemming, Facetting, Highlighting, Spelling
    • Pluggable Architecture
    • 23. Whole Word Matching
    • 24. Loads all indexers
    • 25. Multiple Index Hooks
    • 26. Stored fields
    • 27. Django-like Syntax
    • 28. Templates & Tags
    • 29. Views, Forms & Fields
    • 30. Wildcard Matching
    • 31. Partial word matching
    • 32. Doesn't Load All indexers
    • 33. Interactive shell
    • 34. Close to the metal ( Control )
    • 35. Watches Models for changes
    • 36. Pre-index Filtering
  • REST API
  • 37. REST API
    Exposing Your Data
  • 38. REST API
    Exposing Your Data
    ( In a meaningful way )
  • 39. Django
    defview_func( reqeuest, *args, **kwargs): request.GET request.POST request.FILES
    Problems:
    • PUT & DELETE not translated
    • 40. Can't restrict access based on HTTP methods
    • 41. Serialization is left up to you
    • 42. Manual auth
    • 43. Tons of URLs
  • PISTON
    bitbucket.org/jespern/django-piston
    TASTYPIE
    toastdriven.github.com/django-tastypie/
  • 44. Piston
    classMyHandler( BaseHandler ):
    methods_allowed =( 'GET', 'PUT')
    model = MyModel
    classMyOtherHandler( BaseHandler ):
    methods_allowed =( 'GET', 'PUT')
    model = MyOtherModel
    fields = ('title','content',('author',('username',) ) )
    exclude = ('id', re.compile(r'^private_'))
    defread( self, request):
    return [ x for x in MyOtherModel.objects.select_related() ]
    defupdate( self, request ):
    ...
  • 45. Tastypie
    classMyResource( ModelResource ):
    fk_field = fields.ForiegnKey( OtherResource, 'fk_field' )
    classMeta:
    authentication = ApiKeyAuthentication()
    queryset = MyModel.object.all()
    resource_name = 'resource'
    fields = ['title', 'content', ]
    allowed_methods = [ 'get' ]
    filtering = {
    'somfield': ('exact', 'startswith')
    }
    defdehydrate_FOO( self, bundle ):
    return bundle.data[ 'FOO' ] = 'What I want'
  • 46. Tastypie - Client Side
    newRequest.JSONP({
    url:'http://www.yoursite.com/api/resource'
    ,method:'get'
    ,data:{
    username:'billyblanks'
    ,api_key:'5eb63bbbe01eeed093cb22bb8f5acdc3'
    ,title__startswith:"Hello World"
    }
    ,onSuccess: function( data ){
    console.info( data.meta );
    console.log( data.objects ):
    }).send();
    http://www.yoursite.com/api/resource/1
    http://www.yoursite.com/api/resource/set/1;5
    http://www.yoursite.com/api/resource/?format=xml
  • 47. PISTON
    TASTYPIE
    Multiple Formats
    Throttling
    All HTTP Methods
    Authentication
    Arbitrary Data Resources
    Highly Configurable
  • READY IN MINUTES
  • 61. DATABASE
  • 62. DJANGO-SOUTH
    south.aeracode.org
    QUERYSET-TRANSFORM
    github.com/jbalogh/django-queryset-transform
    DJANGO-SELECTREVERSE
    code.google.com/p/django-selectreverse
  • 63. SOUTH
  • 64. SOUTH
    Database Migrations
  • 65. DJANGO
    $ python manage.py syncdb
  • 66. DJANGO
    $ python manage.py syncdb
    >>> You have a new Database!
  • 67. DJANGO
    classMyModel( models.Model):
    relation = models.ForiegnKey( Model2 )
  • 68. DJANGO
    classMyModel( models.Model ):
    relation = models.ForiegnKey( Model2 )
    classMyModel( models.Model ):
    relation = models.ManyToMany( Model2 )
  • 69. DJANGO
    $ python manage.py syncdb
  • 70. DJANGO
    $ python manage.py syncdb
    >>> Sucks to be you!
  • 71. WTF?!
  • 72. DJANGO
    $ python manage.py syncdb
    >>> Sucks to be you!
    Problem:
    • syncdb doesn't really sync your db. Migrations must be done manually.For everyedatabase / set up.
  • SOUTH
    • Migrations are a set of sequential .py filesdb agnosticHandle most relation typesRolls migrations forward Handles Dependancies / Reverse DependanciesCan convert existing apps Overrides existing syncdb command
  • DJANGO
    classMyModel( models.Model ):
    relation = models.ForiegnKey( Model2 )
    classMyModel( models.Model ):
    relation = models.ManyToMany( Model2 )
  • 73. DJANGO
    classMyModel( models.Model ):
    relation = models.ForiegnKey( Model2 )
    classMyModel( models.Model ):
    relation = models.ManyToMany( Model2 )
  • 74. SOUTH
    $ python manage.py schemamigration <yourapp>
    >>> Sweet, run migrate
  • 75. SOUTH
    $ python manage.py migrate <yourapp>
    >>> Done.
  • 76. SOUTH
  • 77. QUERYSET-TRANSFORM
    github.com/jbalogh/django-queryset-transform
    ( n + 1 )
  • 78. QUERYSET-TRANSFORM
    {%for object in object_list %}
    {%for object in object.things.all %}
    {%if object.relation %}
    {{ object.relation.field.text }}
    {%else%}
    {{ object.other_relation }}
    {%endif%}
    {%endfor%}
    {%empty%}
    no soup for you
    {%endfor%}
  • 79.
  • 80. QUERYSET-TRANSFORM
    deflookup_tags(item_qs):
    item_pks = [item.pk for item in item_qs]
    m2mfield = Item._meta.get_field_by_name('tags')[0]
    tags_for_item =
    Tag.objects.filter(
    item__in = item_pks)
    .extra(select = {'item_id': '%s.%s' % (
    m2mfield.m2m_db_table(),
    m2mfield.m2m_column_name()
    )
    })
    tag_dict = {}
    for tag in tags_for_item:
    tag_dict.setdefault(tag.item_id, []).append(tag)
    for item in item_qs:
    item.fetched_tags = tag_dict.get(item.pk, [])
  • 81. QUERYSET-TRANSFORM
    qs = Item.objects.filter(
    name__contains = 'e'
    ).transform(lookup_tags)
  • 82. QUERYSET-TRANSFORM
    from django.db import connection
    len( connection.queries )
    >>> 2
  • 83. DJANGO-SELECTREVERSE
    code.google.com/p/django-selectreverse
  • 84. DJANGO-SELECTREVERSE
    Tries prefetching on reverse relations
    model_instance.other_model_set.all()
  • 85. CONTENT MANAGEMENT
  • 86. DJANGO-CMS
    www.django-cms.org
    WEBCUBE-CMS
    www.webcubecms.com
    SATCHMO
    www.satchmoproject.com
  • 87. WEBCUBE-CMS
  • 88. WEBCUBE-CMS
    Feature Complete
  • 89. WEBCUBE-CMS
    Feature Complete
    Robust & Flexible
  • 90. WEBCUBE-CMS
    Feature Complete
    Robust & Flexible
    ( Commercial License )
  • 91. $12,000
  • 92. $12,000
  • 93.
  • 94. + $300 / mo
  • 95. WTF?!
  • 96. DJANGO-CMS
  • 97.
  • 98. PRO
    CON
  • SATCHMO
  • 111. SATCHMO
    E-Commerce-y CMS
  • 112. Django Admin
  • 113. DJANGO-GRAPPELLI
    code.google.com/p/django-grappelli
    DJANGO-FILEBROWSE
    code.google.com/p/django-filebrowser
    DJANGO-ADMIN TOOLS
    bitbucket.org/izi/django-admin-tools
  • 114. GRAPPELLI
  • 115. GRAPPELLI
  • 116. GRAPPELLI
  • 117. FILEBROWSER
  • 118. FILEBROWSER
  • 119. FILEBROWSER
  • 120. FILEBROWSER
  • 121. ADMIN TOOLS
  • 122. ADMIN TOOLS
  • 123. ADMIN TOOLS
  • 124. Image Management
  • 125. DJANGO-IMAGEKIT
    bitbucket.org/jdriscoll/django-imagekit
    DJANGO-PHOTOLOGUE
    code.google.com/p/django-photologue
    SORL-THUMBNAIL
    thumbnail.sorl.net/
  • 126. DJANGO
    classMyModel( models.Model ):
    image = models.ImageField(upload_to='/' )
  • 127. DJANGO
    classMyModel( models.Model ):
    image = models.ImageField( upload_to='/' )
    thumb = models.ImageField( upload_to='/' )
  • 128. DJANGO
    classMyModel( models.Model ):
    image = models.ImageField( upload_to='/' )
    thumb = models.ImageField( upload_to='/' )
    >>> MyModel.objects.get(pk=1)
    >>> MyModel.objects.image.url
    >>> MyModel.objects.thumb.url
  • 129. ImageField
  • 130. ImageField
    It Kinda Sucks
  • 131. IMAGEKIT
    Evolution Of Photologue
  • 132. IMAGEKIT
    classPhoto( ImageModel ):
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='photos')
    views = models.PositiveIntegerField(default=0)
    classIKOptions:
    #define the ImageKit options
    spec_module = 'myapp.specs'
    cache_dir = 'photos'
    image_field = 'image'
    save_count_as = 'views'
  • 133. IMAGEKIT
    classResizeThumb( processors.Resize ):
    width = 100
    height = 75
    crop = True
    classResizeDisplay( processors.Resize ):
    width = 600
    # now we can define our thumbnail spec
    classThumbnail( ImageSpec ):
    access_as = 'thumbnail_image'
    pre_cache = True
    processors = [ ResizeThumb ]
    classDisplay( ImageSpec ):
    increment_count = True
    processors = [ ResizeDisplay ]
  • 134. IMAGEKIT
    $ photo = Photo.objects.get( pk =1 )
    $ photo.display.url
    >>> u'/path/to/media/photo_display.jpg'
    $ photo.thumbnail.width
    >>> 100
  • 135. IMAGEKIT
    PHOTLOGUE
    Caching
    Template Friendly
    Highly Configurable
    Management Commands
  • EXIF TAGS
    ISSUE 153
    ( shameless plug )
  • 148. IMAGEKIT
    PHOTLOGUE
    Caching
    Template Friendly
    Highly Configurable
    Management Commands
    Beware of View Counts
  • 160. ImageField
  • 161. ImageField
    It's Kinda Messy
  • 162. SORL
  • 163. SORL
    from sorl.thumbnail import ImageField
    classMyModel( models.Model ):
    iamge = ImageField( upload_to="/folder" )
  • 164. SORL
    {% load thumbnail %}
    {%thumbnail obj.image "1000x600" crop='top' as im %}
    <img src="{{ im.url }}" />
    {%endthumbnail%}
  • 165. MESSAGING
  • 166. User - to - User
    Messaging
  • 167. DJANGO-POSTMAN
    bitbucket.org/psam/django-postman
    DJANGO-MESSAGES
    code.google.com/p/django-messages
    DJANGO-SMS
    code.google.com/p/django-sms
  • 168. DJANGO-SMS
  • 169. DJANGO-SMS
    User Enters Phone #
    User Selects Carrier
  • 170. THATS IT
  • 171. FREE
  • 172. UTILS
  • 173. DJANGO-EXTENSIONS
    github.com/django-extensions/django-extensions
    DEBUG-TOOLBAR
    github.com/robhudson/django-debug-toolbar
    DJANGO-MAINTENANCEMODE
    pypi.python.org/pypi/django-maintenancemode
    DJANGO-PAGINATION
    github.com/ericflo/django-pagination
    DJANGO-GUARDIAN
    github.com/lukaszb/django-guardian
    HOTSAUCE
    ( self plug )
  • 174. DJANGO-EXTENSIONS
    21 Commands
    4 Fields
    2 Models
    1 Admin Extension ( FK AutoComplete )
    $ python manage.py dumpscript
    >>> your_app.py
    $ python manage.py runscript
    >>> your_app.pyc
    $ python manage.py runserver_plus
    >>> Debugger baked right in
    $ python manage.py shell_plus
    >>> AutoLoad Models
    $ python manage.py show_urls
    >>> /your/endpoint/:idproject.app.views.view_func
  • 175. RUNSERVER_PLUS
  • 176. DEBUG TOOLBAR
  • 177. DEBUG TOOLBAR
  • 178. MAINTENANCEMODE
  • 179. MAINTENANCEMODE
    MAINTENANCE_MODE = True
  • 180. MAINTENANCEMODE
    MAINTENANCE_MODE = True
  • 181. MAINTENANCEMODE
    MAINTENANCE_MODE = True
  • 182. HOTSAUCE
  • 183. HOTSAUCE
    I Put That $*!T On Everything
  • 184. HOTSAUCE
  • 185. HOTSAUCE
  • 186. HOTSAUCE
    QueueItem
    ChangeSet
    title
    content
    Model A
    ChangeSet
    title
    content
    ChangeSet
    Model A
    ChangeSet
    title
    content
  • 187. HOTSAUCE