Django

Code

Ticket #927 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

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

Reported by: L.Plant.98@cantab.net Assigned to: adrian
Milestone: Component: Metasystem
Version: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Change History

11/27/05 06:37:03 changed 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.

12/06/05 16:47:53 changed by hugo

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

12/06/05 16:48:33 changed by hugo

#963 it was, sorry ...

12/07/05 10:07:11 changed by anonymous

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

12/07/05 10:51:58 changed 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.

12/07/05 13:39:01 changed 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.

12/08/05 14:59:47 changed by anonymous

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

12/08/05 19:46:14 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

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


Add/Change #927 (non-integer primary keys are broken since new-admin merge)




Change Properties
Action