GenericForeignKey design issue

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

GenericForeignKey design issue


I've been looking into how generic relations work because I wanted an
easier way to use them in the admin interface.  I've written some
javascript and a little bit of python to make generic relationships
quite usable now.

However, one thing that I noticed was that in order for my code to work
properly, the model of interest must have 2 fields; 'content_type' and
'object_id'.  But then I started to think that requiring a field to
have a certain name, without the ability to manually specify which
field is the 'object_id' and which is the 'content_type', was perhaps
something that has overlooked?

For example, consider (from the docs):

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = models.GenericForeignKey()

behind the scenes Django automatically does the following:

class GenericForeignKey(object):
    def __init__(self, ct_field="content_type", fk_field="object_id"):

It seems like it should work like this:

a = models.ForeignKey(ContentType)
b = models.PositiveIntegerField()
content_object = models.GenericForeignKey(ct_field='a', fk_field='b')

and then, for example,

class GenericForeignKey(object):
    def __init__(self, ct_field, fk_field):
        self.ct_field = ct_field or "content_type"
        self.fk_field = fk_field or "object_id"

I'd be willing to do more work on what I've written to make using
generic relationships within the admin if interested.

 You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at