-def create_intermediary_table_model(model):
- """Create an intermediary table model for the specific tag model"""
- name = model.__name__ + 'Relation'
-
- class Meta:
- db_table = '%s_relation' % model._meta.db_table
- unique_together = (('tag', 'content_type', 'object_id'),)
- app_label = model._meta.app_label
-
- def obj_unicode(self):
- try:
- return u'%s [%s]' % (self.content_type.get_object_for_this_type(pk=self.object_id), self.tag)
- except ObjectDoesNotExist:
- return u'<deleted> [%s]' % self.tag
-
- # Set up a dictionary to simulate declarations within a class
- attrs = {
- '__module__': model.__module__,
- 'Meta': Meta,
- 'tag': models.ForeignKey(model, verbose_name=_('tag'), related_name='items'),
- 'content_type': models.ForeignKey(ContentType, verbose_name=_('content type')),
- 'object_id': models.PositiveIntegerField(_('object id'), db_index=True),
- 'content_object': GenericForeignKey('content_type', 'object_id'),
- '__unicode__': obj_unicode,
- }
-
- return type(name, (models.Model,), attrs)
-