On 5/8/07, Suraj <[EMAIL PROTECTED]> wrote:
>
> Hi,
>     I am trying to use a legacy database in Django. The application
> has read only access to this database. This database contains
> ForeignKey fields which don't fit Django's format of <field_name>_id.
> Is there any way to specify the ForeignKey fields name?

Yes: use the db_column attribute on your ForeignKey definition. For example:

   myref = models.ForeignKey(OtherModel, db_column='foo')

will use 'foo', rather than 'myref_id' as the column name

>     Will same problem occur for ManyToMany fields? I have not started
> creating models of those tables yet. But the database has it's own
> naming convention for relationship tables used for holding ManyToMany
> fields.

The same problem will exist, but at present, there is only a partial solution.

You can specify an alternate name for the m2m table using the db_table
argument; however, the column names on this table cannot currently be
modified.

If you're feeling enthusiastic, a fix to overcome this limitation
would be accepted, and shouldn't be too difficult to write. Look at
django.db.models.field.related.ManyToManyField, follow the lead of the
_get_m2m_db_table(), and modify the _get_m2m_column_name() and
_get_m2m_reverse_name() methods. Don't forget to include plenty of
unit tests to validate your behaviour :-)

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to