Code

Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#927 closed defect (fixed)

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

Reported by: L.Plant.98@… Owned by: adrian
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: UI/UX:

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.

Attachments (0)

Change History (8)

comment:1 Changed 8 years ago by rjwittams

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 Changed 8 years ago by hugo

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

comment:3 Changed 8 years ago by hugo

#963 it was, sorry ...

comment:4 Changed 8 years ago by anonymous

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

comment:5 Changed 8 years ago by anonymous

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 Changed 8 years ago by rjwittams

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 Changed 8 years ago by anonymous

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

comment:8 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

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.