| 452 | def refresh_foreign_keys(self): |
| 453 | """ |
| 454 | Refreshes all foreign keys. Should be used before saving. If the primary |
| 455 | key of a foreign key changed after setting the related object to the |
| 456 | model it can not be refreshed automatically. |
| 457 | """ |
| 458 | |
| 459 | # Get all foreign keys of this model. Attname and name must be equal. |
| 460 | # attname.......Presents the property of this model in which field the |
| 461 | # duplicated primary key is saved in the database. |
| 462 | # name..........Presents the reference to the object where the original |
| 463 | # primary key is saved. |
| 464 | fks = [(f.attname, f.name) for f in self._meta.local_fields if type(f) == ForeignKey] |
| 465 | |
| 466 | for (db_ref, fk_ref) in fks: |
| 467 | # If db_ref already exists we do not make unneccessary db-queries, |
| 468 | # because the related object is already set in the correct. |
| 469 | if not getattr(self, db_ref) and hasattr(self, fk_ref): |
| 470 | |
| 471 | # raise an error if a primary key of the related object does |
| 472 | # not exist (if it is not saved until now) |
| 473 | if getattr(self, fk_ref).pk == None: |
| 474 | print type(str(fk_ref)) |
| 475 | raise ValueError("Cannot find a primary key for " + str(fk_ref) + |
| 476 | " as a foreign key in an instance of " + self.__class__.__name__ + |
| 477 | ". Have you already saved the " + str(fk_ref) + " object?") |
| 478 | |
| 479 | # If the duplicated key is not the real one, set the real one |
| 480 | if getattr(self, db_ref) != getattr(self, fk_ref).pk: |
| 481 | setattr(self, db_ref, getattr(self, fk_ref).pk) |
| 482 | |