How do I interface with a Django model containing a "model" field?

17 views
Skip to first unread message

Tim

unread,
Jul 24, 2009, 4:39:14 PM7/24/09
to django-piston
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.

Tim

unread,
Jul 28, 2009, 12:24:50 PM7/28/09
to django-piston
I was able to solve this problem by changing the method_fields
function in emitters.py to use

for field in fields:
if field in has and callable(field):
ret[field] = getattr(data, field)

instead of

for field in fields:
if field in has:
ret[field] = getattr(data, field)

It seems to work in this case - is this going to break other things,
though?

jespern

unread,
Jul 30, 2009, 3:44:32 AM7/30/09
to django-piston
I don't think this would break anything else. Do you want to
contribute a patch, or do you want me to fix it? Also, what's your
full name, so I can add it to AUTHORS.txt.


Jesper
Reply all
Reply to author
Forward
0 new messages