Query a Random Row With Django

Tuesday, May 14 2013 5:38 p.m.

Here's a gist for a drop-in Django manager class that allows you to return a random row.

Model.objects.random()

It can be used in your models.py like this:

class QuoteManager(RandomManager):
    def random_filter(self):
        return self.filter(is_active=True)

class Quote(models.Model):
    quote = models.TextField()
    by = models.CharField(max_length=75)
    is_active = models.BooleanField(default=True)

    objects = QuoteManager()

    def __unicode__(self):
        return self.by

Advantages over using the order_by('?') is performance. Random sort at the database seems to be extremely slow on most databases even if the table only has a few thousand rows. Note that the count of records is cached for 5 minutes, so if the table changes often you may want to change that. A limitation is that it only returns one row.

django, programming, python
Comment




Box Office Champs Launches

Thursday, May 2 2013 11:05 a.m.

A fantasy movie game built using Django. This is a very easy game where you pick the 15 movies you think will be the highest grossing movies of the season. You can create a group and compete with your friends.

You should give the site a try today. Iron Man 3 opens tomorrow and you definitely want to have that movie on your roster. Unlike fantasy sports, you can play this game 4 times a year. The summer season starts tomorrow.

Kudos to Rudy Menendez who did principal development and game design and Noah Wenz for design and HTML. The site was with spare time over the last few months. Put together, development time was about 2-3 weeks.

If you need a fantasy site done, contact Ed or Rudy Menendez and we can help you out.

digitalhaiku, django, fantasy, python
Comment




Getting AngularJS Authentication Working with Django

Thursday, March 28 2013 2:24 p.m.

django-angular-auth is a very simple example app or seed to get AngularJS authenticating using Django and Tastypie.

Although AngularJS' documentation has gotten much better, it still took me quite a while to figure out what exactly was the best path to take. Also, there are a few gotchas with allowed headers and cross-site security which are already solved in the example. Take a look and let me know what you think.

angularJS, django, programming
Comment




LittleBrownieBakers.com Launches

Thursday, November 17 2011 8:39 p.m.

Little Brownie Bakers are the bakers that make Girl Scout Cookies. They required a custom CMS where they could enter info about cookies, post selling tips for parents, kids and volunteers; and generally update the world on the latest cookie news. And now they have it...

digitalhaiku, django, programming, python
Continue reading




BestBuy Fantasy Footaball Launches

Wednesday, September 21 2011 11:59 a.m.

Launched back before NFL week 1. But, that was DjangoCon 2011, so the post happens today.

Best Buy's site is operating for the third year in a row. Originally it was open to the public, but now it is employee only. You play by selecting a line-up every week and play against your co-workers and against the CEO. Makes for really fun team building.

digitalhaiku, django, fantasy, python
Continue reading




US Open CourtConnect Launch!

Tuesday, September 6 2011 4:45 p.m.

The US Open CourtConnect site allows you to watch in real-time what people are talking about on Twitter and Facebook regarding the US Open. You can see personal photos taken right at the event by the general public, as well as those tweeted by players, experts and celebrities. CourtConnect takes the tweets and status updates and automatically color commentates by attaching facts relevant to the keywords in the message. It also highlights experts, players and celebrity tweets so you know who is who.

CourtConnect was built in Python using Django 1.3 and jQuery using Masonry in a very compressed timeline by Rudy Menendez, the team at Blenderbox and myself. We were able to use APIs from Mass Relevence and Pusher to push this site through that timeline. Other cloud services used are Embedly and EC2.

digitalhaiku, django, programming, python, realtime
Continue reading




Voxy.com Launch!

Friday, May 6 2011 7:30 a.m.

Voxy helps you learn a language from life. That means, doing things you would do anyway but learn a language while you do it. Like reading about the NFL lock-out in Spanish. If you're learning English, an English version is also available here.

The iPhone app has reached #1 in the AppStore for education apps in 14 countries. Android app is coming any day now too. Both apps support location based learning. Are you near a bank and need to figure how to linguically maneuver through a transaction? Voxy can help!

digitalhaiku, django, programming, python
Continue reading




National Geographic Education Site Launch!

Monday, April 18 2011 9 a.m.

National Geographic creates educational programs, reference material and news used by teachers, students and parents in the United States and across the world to promote geo-literacy. And now this content is available online using a custom CMS built with Django.

digitalhaiku, django, programming, python
Continue reading




Using Django as a Pass Through Image Proxy

Sunday, March 21 2010 8:39 p.m.

An earlier post shows you how to setup Nginx as a pass through image proxy. This post shows you how to do it with just Django and nothing else.

The Problem

We've solving the same problem as the earlier post. However, I will repeat it here for clarity as there's been some confusion.

You have a production DB with lots of images uploaded by users. For example, NationalGeographic.com has over 11gb of user uploaded images. When you download a data dump of the production database, it has links to all these images which you don't have. You either have to download and sync all the images locally every time you copy the database, live with broken images or point your static images to the prod server.

django, programming, python
Continue reading




Maintain Constants.py through Data Migration

Sunday, March 14 2010 7:49 p.m.

This post will show you how to create a local_contants.py file using Django South. If you're not familiar with South, it's a data migration tool. Stated differently, it's a tool that helps keep your database changes current across different sandboxes and server environments.

django, programming, python, south
Continue reading




Configuring Nginx as a Pass Through Image Proxy

Saturday, March 13 2010 8:26 p.m.

This post shows how to setup Nginx has a pass-through image proxy server. This is useful for local sandboxes, UAT and QA servers where you copy production database down to but don't want to copy all the user generated images to the non production environment.

django, programming
Continue reading




Efficiently Test Emails in Django

Sunday, July 19 2009 6:08 p.m.

Testing emails can be an inefficient process. The email needs to leave your machine, arrive at the SMTP server, get forwarded to your test mail server and then downloaded by your email client. Do you want those 30 seconds of your life back?

Using an SMTP sink, you can receive the email to your screen instantly. Setup is easy.

  •  ♦  Download smtp_sink.py
  •  ♦  Put it in whatever your project directory is. You will want to create a "inbox" directory. Besides outputting to the screen, this will automatically copy the email to the inbox directory in case you need to reference it later
  •  ♦  Change your settings.py to have: EMAIL_HOST = '127.0.0.1', EMAIL_HOST_USER = '', EMAIL_PORT = 25

If you have a script that loads your sandbox environment automatically, you will want to load smtp_sink.py automatically. It will send a test signal to see if it's already running and not start twice.

If you run automated test scripts, the output of the "inbox" can be tested.

django, programming, python
Comment




AvaTint Launched!

Tuesday, June 30 2009 11 p.m.

The site very simply takes your avatar and turns it into any color you select. The online community is starting to do this to support and protest many things. This is the 2.0 way of making your page background black for a cause.

We created this site in 90 minutes using Django on June 19th.

django, programming, python
Continue reading




Easily Code Templates for iPhone in Django

Monday, May 4 2009 9:49 p.m.

Sometimes, you want to create a custom page for an iPhone or other small phone browser but don't want a custom URL or view for it. This can easily be handled by using the mini_render_to_response function.

django, programming, python
Continue reading




Hollywood Draft Private League Launch!

Thursday, April 23 2009 7:52 a.m.

This new game allows you to play in a more traditional fantasy game model where you draft from a pool celebrities. This allows fun things like draft parties, benching players and all the regular things those familiar with fantasy sport already know.

digitalhaiku, django, fantasy
Continue reading




MyEmailDraft.com Launched!

Wednesday, April 22 2009 10 a.m.

The site solves one simple problem. Messy email based fantasy drafts. Our goal was to make something as simple to setup as an email draft but without any of the downside.

django, fantasy
Continue reading




Celebrity Fantasy Game in Django

Monday, August 18 2008 9:53 p.m.

If you've played fantasy football or any fantasy sport you know what I'm takling about. If you're new to the concept, you pick a roster of celebrities and if they show up in the news, you get points.

The site was written using Django (trunk as of the 13th!) and Postgres 8.2. We've built a fantasy games package on top of the Django core which we would probably open-source if the community showed some interest in it.

This is our 2nd major site in Django and we really love it. To begin with, Python is a great language. When we built our fantasy engine, it was originally coded on MySQL. We encountered quite a few problems porting it over to Postgres. There are lots of little things where MySQL does type conversions for you automatically and Postgres (correctly in my opinion) does not. I definitely feel the conversion was worth it so that now we're on an enterprise level database.

django, programming, python
Continue reading




MySQL Connection Pooling with Django and SQLAlchemy

Friday, July 25 2008 12:41 p.m.

Here's a quick and dirty recipe to get connection caching from SQLAlchemy. This is really not connection pooling as that would require a separate process to only handle connections. All this does is prevent the connection from closing after you finish a query. We're also not replacing the Django ORM.

To give a little more background, normally when you use Django to get to your database, Django will automatically disconnect from the database when the thread is done with that query.

django, programming, python
Continue reading




Launching a High Performance Django Site

Sunday, July 6 2008 3:29 p.m.

Are the brakes on your Django app?

When building an application using an application framework like Django... the priority is often to get the application working first and optimize it later. The trade off is between getting it done and getting it done for 1 million users. Here's a check list of things you can do to make sure your application can be optimized quickly when you put on your optimization hat. Note, most applications don't need all of this since most applications do not get anywhere near enough traffic to justify even bothering. But if you're lucky enough to need to optimize your Django app, I hope this post can help you.

django, programming, python
Continue reading




MySQL, Dates and Time zones, Postgres and Mindshare

Saturday, May 10 2008 12:27 a.m.

A database can’t be all things to everyone. Each one has its sweet spot. MySQL happens to be the Wal-Mart of the database world. A large market sweet spot with lots of really cheap stuff, questionably good for you and serving a basic need.

Occasionally, MySQL will let you down with some basic things… like time zones.

django, mysql, programming
Continue reading




New Site. Now With 80% More Blog

Thursday, May 1 2008 11:56 p.m.

Hope everyone likes the new updated blog format. Been trying to get this online for several months, but never had enough time to get it just right. It's still not, but it's good enough.

Complete change in technology for the new version. Using Django app framework on top of Python. Python is the most fun coding since SQLWindows (another OOP language with forced language formatting).

Features coming soon... RSS feeds and tagging.

django, programming
Comment




Subscribe RSS Feed

More Blog Entries

Ed Menendez' Facebook profile