#13527: Allow column on ForeignKey
------------------------------------------+---------------------------------
 Reporter:  malthe                        |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  1.1       
 Keywords:                                |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 Currently, on the many-side of a `ManyToOne` relation, the underlying
 value of the relation is always stored in `fieldname_id` (both on the
 database side and in the ORM). However, this value will often be
 meaningful on its own and it should be possible to explicitly provide a
 name, e.g. `from_field` (the database column name would be
 "%(from_field)s_id".)

 The following implements this:

 {{{
 class CustomForeignKey(models.ForeignKey):
     def __init__(self, *args, **kwargs):
         self.from_field = kwargs.pop('from_field')
         if self.from_field:
             kwargs.setdefault('db_column', "%s_id" % self.from_field)
         return super(CustomForeignKey, self).__init__(*args, **kwargs)

     def get_attname(self):
         return self.from_field or "%s_id" % self.name
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/13527>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" 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-updates?hl=en.

Reply via email to