Created
February 13, 2012 15:38
-
-
Save ruiwen/1817670 to your computer and use it in GitHub Desktop.
Enable django-piston to POST/PUT models with ForeignKeys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten_dict(self, dct): | |
out = {} | |
for field in dct.keys(): | |
fk = self.model._meta.get_field(field) | |
if isinstance(fk, ForeignKey): | |
# Determine the target model of the ForeignKey | |
Klass = fk.rel.to().__class__ | |
d = { Klass._meta.pk.name : dct[field] } | |
# Grab the instance | |
i = Klass.objects.get(**d) | |
out[field] = i | |
else: | |
out[field] = dct[field] | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment