Ticket #11265: new_id_attribute_name_kwarg.patch

File new_id_attribute_name_kwarg.patch, 1004 bytes (added by dstora, 15 years ago)

Patch

  • django/db/models/fields/related.py

     
    660660class ForeignKey(RelatedField, Field):
    661661    empty_strings_allowed = False
    662662    def __init__(self, to, to_field=None, rel_class=ManyToOneRel, **kwargs):
     663        self.__id_attr_name = kwargs.pop('id_attr_name', None)
    663664        try:
    664665            to_name = to._meta.object_name.lower()
    665666        except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT
     
    679680        self.db_index = True
    680681
    681682    def get_attname(self):
    682         return '%s_id' % self.name
     683        if self.__id_attr_name:
     684            return self.__id_attr_name
     685        else:
     686            return '%s_id' % self.name
    683687
    684688    def get_validator_unique_lookup_type(self):
    685689        return '%s__%s__exact' % (self.name, self.rel.get_related_field().name)
Back to Top