Opened 19 years ago

Closed 19 years ago

Last modified 18 years ago

#927 closed defect (fixed)

non-integer primary keys are broken since new-admin merge

Reported by: L.Plant.98@… Owned by: Adrian Holovaty
Component: Metasystem Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

From "svn annotate django/core/meta/fields.py", line 721 onwards:

  1434     adrian     def get_db_prep_save(self,value):
  1434     adrian         if value == '' or value == None:
  1434     adrian            return None
  1434     adrian         else:
  1434     adrian            return int(value)

That "return int(value)" obviously is not compatible with non-integer primary keys. The problem occurs outside of the admin - i.e. when creating objects and setting foreign keys manually in my migration script.

Change History (8)

comment:1 by rjwittams, 19 years ago

So the right fix here needs to take the rel.get_related_field() into account, as it will have the same type of value as that. Maybe delegate to the db_prep_save of that field. But it also needs to take related fields being null when the field it is related to cannot be.

comment:2 by hugo, 19 years ago

#968 and #1010 where duplicates and might include additional information.

comment:3 by hugo, 19 years ago

#963 it was, sorry ...

comment:4 by anonymous, 19 years ago

It appears that the to_field in the FK cannot be non-integer.

comment:5 by anonymous, 19 years ago

As per rjwittams suggestion, I have changed my local copy to:

def get_db_prep_save(self,value):

return self.rel.get_related_field().get_db_prep_save(value)

And of course it does work, now. About the issue with fields being null, wouldn't the delegated method for the model field
check that automatically? Pardon my ignorance, as I am just begining to dig into the internals.

comment:6 by rjwittams, 19 years ago

Hey anonymous,

The point is that a ForeignKey field could be null, so that needs to be an accepted value. Any primary key cannot be null, so would always complain.
So

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

is probably the right thing for now.

comment:7 by anonymous, 19 years ago

I use custom alphanumeric pks and rjwittams fix worked for me. Thanks!

comment:8 by Adrian Holovaty, 19 years ago

Resolution: fixed
Status: newclosed

(In [1569]) Fixed #927 -- Non-integer primary keys save() method now works

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