Generic Relation in Legacy Database?

79 views
Skip to first unread message

Nan

unread,
Mar 8, 2015, 11:31:28 AM3/8/15
to django...@googlegroups.com

Hi folks --

I'm building out Django models to represent a legacy PHP app's database in order to throw together a quick admin panel.  Everything's working great except that the ORM of the other application represents generic relations slightly differently than Django's does.  I can't modify the DB schema because the legacy application has to continue to run.

Django uses:

int content_type_id
int object_id

The existing DB essentially uses:

string model_class_name
int object_id

Is there a way to get Django's ORM to use these fields to represent a GFK relationship so I can use it in the admin?  I can add tables, just not modify existing ones.  Maybe an extra 1-to-1 mapping content types to legacy model names would help?  Maybe writing a GenericForeignKey subclass?  A quick look at the source suggests there are a lot of methods that will need to be overridden (by copy-pasting 40 lines and changing one), which doesn't sound like fun to maintain in the long run.

Just wondering if anyone's done this before or has any tips before I dive in.

Thanks!

Avraham Serour

unread,
Mar 8, 2015, 1:07:56 PM3/8/15
to django...@googlegroups.com
use inspectdb to start

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3ec838f1-34ff-46f1-9b94-d3b9ac905d19%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nan

unread,
Mar 10, 2015, 8:59:01 AM3/10/15
to django...@googlegroups.com

Thanks, Avraham -- I was planning to use inspectdb as a first step, and it's very handy in streamlining building models!

But that doesn't even begin to solve the problem of how a GFK modeled differently than Django's ORM does can be used as an actual foreign key.

Avraham Serour

unread,
Mar 10, 2015, 10:48:19 AM3/10/15
to django...@googlegroups.com
for each field in the model you can specify the column name un the db, it does have a default way of calculating that, but you can specify

in any case you can tell the django ORM that this field is not an int, it is actually a foreign key, let me know if that works

what DB are you using? mysql?

ring...@gmail.com

unread,
Mar 10, 2015, 10:57:41 AM3/10/15
to django...@googlegroups.com

Avraham, are you at all familiar with Django’s GenericForeignKey model field?  

It relies on *two* fields in the model.  Django resolves a generic foreign key by looking up the content type (a FK to the content types table) to figure out what table to look for the object ID in.  Then it looks for the object ID (what would normally be the foreign key) in the table in question.

The problem is not the object ID.  The problem is that my table does not reference Django’s content types table and thus does not conform to what Django expects from a GenericForeignKey.  Django then won’t be able to tell which table to look up the object ID in.

And I’m using Postgres, but that shouldn’t matter in this case.


You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/CRGUNH0DkhU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Gergely Polonkai

unread,
Mar 10, 2015, 3:31:31 PM3/10/15
to django...@googlegroups.com

Hello,

By taking a quick glance on I'm afraid that a GFK subclass is your best bet. I was experimenting with a Doctrine DB (based on your description my guess was that your old ORM may be that). A little bit of change to GFK made the query working, although I haven't solved the insert/update problem yet.

Best,
Gergely

ring...@gmail.com

unread,
Mar 10, 2015, 9:08:29 PM3/10/15
to django...@googlegroups.com

Thanks, Gergely — it’s actually Eloquent, which handles GFKs a little more similarly to Django than Doctrine does, AFAICT.

Would you mind sharing your GFK changes-in-progress?  Maybe we could collaborate on a broader solution.


Gergely Polonkai

unread,
Mar 11, 2015, 3:02:23 AM3/11/15
to django...@googlegroups.com

Hello,

sure, I can do that as soon as I get home in the evening (timezone CET). Maybe we can patch up some helper class for anyone who wants to migrate from eloquent, although I don't think many devs who use an ORM already would want to transition to another, but hey, who knows? :)

Best,
Gergely

Reply all
Reply to author
Forward
0 new messages