Code

Opened 4 years ago

Closed 4 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: UI/UX:

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)

Attachments (0)

Change History (1)

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.