Ticket #18501: db.models.fields.related-fix-subclassed.patch

File db.models.fields.related-fix-subclassed.patch, 1.3 KB (added by Melvyn Sopacua, 12 years ago)

Patch to make subclassed fields work as forein key

  • db/models/fields/related.py

     
    124124        if not cls._meta.abstract:
    125125            self.contribute_to_related_class(other, self.related)
    126126
     127    def value_from_object(self, obj) :
     128        """
     129        If the foreign key is a subclassed field, we need to convert the value
     130        to its python representation. This function is called by model_to_dict,
     131        which presents the values as a dictionary to the form.
     132        getattr() will put the database value on the dict by default as that is
     133        what the real relationship represents. In the common (unsubclassed)
     134        case, they will be the same value, however if subclassed this results
     135        in:
     136        a) get_prep_value on the custom model field being called with database
     137           value. Working around this and just returning the value results in:
     138        b) Select widget not being able to match the database representation
     139           with the python representation.
     140        """
     141        return self.rel.get_related_field().to_python(getattr(obj, self.attname))
     142
    127143    def get_prep_lookup(self, lookup_type, value):
    128144        if hasattr(value, 'prepare'):
    129145            return value.prepare()
Back to Top