Hi,

I need an educated opinion on the following idea. It's supposed to
provide a (very-)weakly coupled architecture where any object that
inherits from ExtendableModel can be said to relate to one or many
objects that also inherit from it. This way I want to achieve at least
two things:

  (1) No need to hard-code relationships
  (2) As a consequence of (1) no need to reconstruct the tables every
time I want to add a new relationship

I think this is not a very bad idea, but maybe technically there are
better ways to implement this. Or, maybe, it IS a bad idea
altogether? :)
Also - how is it (if at all) possible to still use the admin interface
with this kind of architecture?

The code is below - thanks for any comments!

Thanks,
Igor
____________________________________________________

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class ExtendableModel(models.Model):
        id = models.AutoField(primary_key=True)
        extendable_content_type = models.ForeignKey(ContentType)
        extendable_object_id = models.PositiveIntegerField()
        extendable_content_object = generic.GenericForeignKey
('extension_content_type', 'extension_object_id')

        def __init__(self, *args, **kwargs):
                super(ExtendableModel, self).__init__(self, args, kwargs)
                self.extendable_content_type_id = int
(ContentType.objects.get_for_model(self).id)
                print type(self.extendable_content_type_id)

        def get_related(self, model_class): # Returns objects of type
model_class that extend self
                ContentType.objects.get_for_model(b)

                return model_class.objects.filter(
                                
extendable_content_type__pk=ContentType.objects.get_for_model
(self).id,
                                extendable_object_id=self.id)


____________________________________________________

Sample usage:

class Entry(ExtendableModel):
        title = models.TextField(editable=False, blank=True)
        ...

class Event(ExtendableModel):
    start_datetime = models.DateTimeField
(default=datetime.datetime.now)
    end_datetime = models.DateTimeField(default=datetime.datetime.now)

____________________________________________________

entry = Entry(title="A blog post")
event = Event(start_datetime=somestime1, end_datetime_sometime2)

entry.associate(event) # suppose this is how actual object-to-object
relationships
                                 # are added - haven't thought this
thru

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to