I'm using Piston to add an API to a Django application - this is my
first foray into any sort of web programming, so I have had a lot of
learning to do. The non-API portion of the application is working
well, but now I'm trying to add in the ability to have a web service
interface to do things like add new data. I have several Django
models that I am adding handlers for - initially, it worked fine but
then I added a new handler, and everything went downhill.
The Django models in question are:
Result:
variable = models.ForeignKey(Variable)
valid_time = models.DateTimeField()
lead_time = models.IntegerField()
value = models.FloatField()
class Variable(models.Model):
region = models.ForeignKey(Region)
model = models.ForeignKey(Model)
method = models.ForeignKey(Method)
kind = models.ForeignKey(Kind)
level = models.ForeignKey(Level)
If I have a handler for Result, everything works fine - as soon as I
added a handler for Variable (even though I wasn't directly using it),
the recursive processing of serializing a Result was throwing an
error. I was able to (at least start to) trace through the Piston
emitters module, and piece out what was going wrong there.
It appears that when a Variable is serialized, the get_fields in the
_model routine are correct (i.e. ('region', 'model', 'method', 'kind',
'level'). However, the method_fields routine detects that the handler
has a 'model' attribute (the Variable instance that the handler is
for) and returns *that*, instead of the Model that it should be. So,
while all the fields are present, 'model' is "incorrectly"* added to
met_fields, which leads to a DoesNotExist exception and a 500 error.
All the above tests were performed using the default field listing -
if I explicitly set a fields attribute in the VariableHandler to
include all the fields in Variable, the error still occurs. If I
manually remove the "model" field from the fields list in
VariableHandler, everything works again.
Is there a way to get around this that doesn't involve renaming my
model fields? This is verification data from a suite of forecast
models, so "model" would otherwise be an appropriate name if it didn't
cause all these issues.
Please let me know if more information would be helpful - like I
mentioned, this is my first attempt to move from numerical computing
in Fortran to Python web applications so any assistance is welcome.
Thank you!
Tim
*"incorrectly" only in the sense that the code is correct and doing
what it was programmed to do - it's just an improper action to take in
this case.