GenericForeignKey design issue

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 relationship
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 been overlooked (maybe this was done

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 something like this instead:

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"

This would also make it possible to have more than one generic relation
per object.

 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