Return Elvis stamps to sender!

Web

If you create a model class in Django and then create a model subclass of that, then Django creates a 1:1 relationship to a table with the extra fields defined in the subclass. This results in there being an instance of the parent class as well as an “extended version” of that object in the subclass. That works out rather well for most cases, but there’s a few edge cases where it’s not sufficient.

Say I have an object A and a subclass B:


class A(Model): created = DateTimeField(auto_now_add=True)

class B(Model): modified = DateTimeField(auto_now=True)

I’ll wind up with some tables like this:

A

id created
1 14535143
2 14536222

B

id modified a_ptr_id
1 34253245 1

What this means is that if I instantiate PK 1 via A’s manager, I’ll get an A and only ever an A because the information about what kind of object it is exists only in B’s table. So if I have, say, a blog engine like Tumblr where there are text, image, video, audio, and other kinds of posts and I’ve made them all model classes based off RootBlog, I can’t do a lookup by PK and expect the right object; I’ll always get a RootBlog object.

Traditionally, if you had this problem, you’d use abstract classes and just have a different object series for each kind of post. That works, but it means you can’t do shortcuts that just pass around an ID and get the right object — you have to pass around the type as well.

However, there is one trick that’s a result of the 1:1 relationship that could be of some assistance here. When you create an instance of our example object A, it will have a property called ‘b’ that is a link to its corresponding full instance of B. The problem is that we can’t easily follow this without giving the ancestor direct knowledge of all its descendants’ names.

Read the rest »

Question Ported

April 16, 2006 - 9:26am

Well, I have to get MG over to 4.7, so I just took some hours out of my night and just finished porting the Drupal Question module over to 4.7. It was quite painful, really, due not only to the way that form creation changed, but also validation and committal. But, after reading a lot and using the work already done on the bug it was doable, and done. Today, I’ll be doing a lot of work on the server to get MG ported over to 4.7 and live.

This should be fun. There’s a ton of things I can do with 4.7 that will really enhance the site.

Read the rest »

“Pride consists in a man making his personality the only test, instead of making truth the test. It is not pride to wish to do well, or even to look well, according to a real test. It is pride to think that a thing looks ill. because it does not look like something characteristic of oneself.” — The Common Man, NY: Sheed & Ward, 1950, 254 – G. K. Chesterton

Syndicate content