Opened 15 years ago

Closed 15 years ago

#13122 closed (invalid)

ForeignKey class calls own get_db_prep_save tests prior to the related field get_db_prep_save

Reported by: SeniorHuevo Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This is a problem when you have created a custom field to use a complex python type and you wish to use that custom field as a primary key.

On line 811 of django/db/models/fields/related.py

    def get_db_prep_save(self, value, connection):
        if value == '' or value == None:
            return None
        else:
            return self.rel.get_related_field().get_db_prep_save(value,
                connection=connection)

This may result in an AttributeError being rased if the python type only compares to the exact same python type.

it's better to let the custom class handle it

    def get_db_prep_save(self, value, connection):
        try:
            if value == '' or value == None:
                return None
        except AttributeError:
            pass

        return self.rel.get_related_field().get_db_prep_save(value,
            connection=connection)

Change History (1)

comment:1 by Russell Keith-Magee, 15 years ago

Resolution: invalid
Status: newclosed

This sounds like a case of a badly written custom field/python type to me. An equals comparison shouldn't be raising AttributeError. I can't think of an example from the Python standard library that would behave like this.

If you can provide an example of a standard Python library object that behaves like this, please reopen and we'll reconsider.

Note: See TracTickets for help on using tickets.
Back to Top