Wednesday, November 28

Ordering related objects in Django

Django models have a meta option order_with_respect_to that allows you to order objects within the scope of a related ForeignKey object.

This option adds an _order column to the model's database table to keep track of this ordering.

That's all very well, but if you need to change the sequence order you may well feel at a loss. The official documentation makes no mention of this option beyond a basic explanation of its purpose, and editing your objects in the built in admin app reveals no user interface for changing the ordering either. [1]

I managed to uncover a Python interface by delving into Django's API internals [2]. An object with related models that order with respect to it is given two handy methods:

get_RELATED_order()
set_RELATED_order()

where RELATED is the lowercased model name of the ordered objects

class Entry(model.Model):
# ... fields

class Comment(model.Model):
entry = model.ForeignKey(Entry, related_name='comments')
# ... more fields

class Meta:
order_with_respect_to = 'entry'

Given the above example models, you can retrieve the order of an entry's comments:

>>> entry = Entry.objects.latest()
>>> entry.get_comment_order()
[1, 2, 3, 4]

And change the ordering by passing a list of comment ids back in.

>>> entry.set_comment_order([2, 1, 4, 3])

N.B. Be sure to pass in the same ids returned by get_comment_order

Two other handy methods exist for the Comment objects, get_next_in_order and get_previous_in_order [3]

>>> comment = Comment.objects.get(pk=3)
>>> comment.get_next_in_order()
<Comment: 4>
>>> comment.get_previous_in_order()
<Comment: 2>


  1. A very early ticket is still open to restore this functionality to the admin app, and recent activity suggests it may well reappear soon. See also this thread on the django-developers mailing list. back

  2. Look particularly at the two helper functions method_set_order and method_get_order in django.db.models.base. back

  3. Theoretically at least, there's still a bug in get_previous_in_order but it should be fixed soon. Update: Fixed by the queryset-refactor back

Labels: ,

Saturday, November 24

Barcelona 11/07 Photos

La Pedrera

Labels:

Sunday, March 18

Bus times back online

LiveBus.org is currently the first hit for "mash up offline" on Google. Not a particularly delightful situation, but I'm happy to say that the DeadBus has risen!

I've moved the site to a sultry new virtual private server at RimuHosting and real time bus updates are flowing once more. You shouldn't notice a difference, but I've now got a lot more freedom to tweak performance behind the scenes, and future downtime of this sort should be rare.
Disclaimer: Rare, not impossible

Do let me know if you're seeing funny stuff, things may have become muddled in the move.

Labels:

Wednesday, February 14

LiveBus.org down

Due to some server configuration issues, I've had to take LiveBus down until further notice. I'll hopefully have the problems solved soon and will make another announcement as soon as the site returns.

Sorry for the annoyance.

Update: The site's back online

Labels:

Thursday, February 8

LiveBus.org launched

As part of a demo at the inaugural Oxford Geek Night (Update: podcasts now online at that link - hooray for immortalised mumblings!) on Wednesday night, I finally gave in and launched the project I've been working on for the last many months.

LiveBus.org is a free service that lets you find bus stops and routes on a map and look up live arrival times. It currently covers most routes in Oxfordshire and Surrey, and will hopefully be expanded in the future.

More info on LiveBus is at the about page.

Comments/questions/suggestions welcomed and encouraged.

Labels:

Wednesday, January 17

How to piss me off...

Insert Corporate Identity Here

Papyrus? Just say no.

Yes, I am a font nerd.

Labels:

Monday, February 27

Pen to paper

I hate technical drawing.

Sitting down at a drawing board with a T-square and scale ruler to draw precise plans, elevations and the like can be a real chore when you're used to living the cosy Moleskine and biro lifestyle. I'd much rather churn out wobbly sketches of spidery landscaping and funkily shaded walls than spend hours accurately measuring out windows and scratching away ink smudges before scrapping my work and starting again because I was out by half a metre.

Alas, this is a necessary evil of the course, even with the modern luxury of eager CAD monkeys slaving away in the dungeons of architectural practice. So here's what I've got to do:
  • Plan
  • Four elevations
  • Section
  • Isometric
  • Sectional perspective showing construction details
  • Model at 1:50 scale
When what I really want to be coming up with is more stuff like this:



Such is the life of a student these days...

Labels: