I'm trying to come up with a way of writing re-useable code for certain
common features on two-point-oh-y sites. This might become a long post,
so apologies in advance.

Say your building the standard blog application. It's conceivable that
you'd want your users to add comments, add tags, and rate posts.

You might have a model for comments which looks slightly like this.

class Comment(models.Model):
        post         = models.ForeignKey(BlogPost)
        author       = models.ForeignKey(User)
        comment_text = models.TextField(blank=True)

something similar for tags and ratings.

Then you expand your site to add your photos, and you'd like to allow
the same features to be used, and, in fact, the same code to be used.
Trouble is, the models you've defined specifies that Comments work with
BlogPosts.

Now looking at the django.contrib.comments code I see that the author
there decided to use an integer object id rather than a ForeignKey

class Comment(models.Model):
        parent       = models.IntegerField(('object ID'))
        author       = models.ForeignKey(User)
        comment_text = models.TextField(blank=True)

Ok, this means that the Comment can be used with any other item
(assuming it has an integer primary key), but we've got BlogPosts and
Photos on our site, and their object ids are not guaranteed to be
unique (in fact they're likely not to be). Also we've lost the nice
django DB API for traversing this relationship.

I can think of a few solutions, but don't know how to implement them.

1) Add a type_id field to the Comment model, which identifies which
table the object_id should be looked up in. I think most SQL databases
have a 'Table of Tables', but my SQL isn't good enough to know if this
is standard or not, and my django isn't good enough to know how to get
at it. Maybe it would need to be the name of the table in a varchar.

2) Have two tables containing Comments (one for BlogPosts and one for
Photos). I don't think django supports this at the moment as 1 class
appears to equal 1 table.

3) implement a 'Super'Key field which behaves like a foreignkey (i.e.
preserves the django DB API), but is able to refer to any object in any
table (possibly by storing info like (1))

Does anybody have any suggestion as to how any of these could be done?
I know this isn't really something that SQL supports, but I'm hopeful
it's possible to fake.

Thanks

Paul


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to