Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How do I interface with a Django model containing a "model" field?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tim  
View profile  
 More options Jul 24 2009, 4:39 pm
From: Tim <twhitc...@gmail.com>
Date: Fri, 24 Jul 2009 13:39:14 -0700 (PDT)
Local: Fri, Jul 24 2009 4:39 pm
Subject: How do I interface with a Django model containing a "model" field?
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim  
View profile  
 More options Jul 28 2009, 12:24 pm
From: Tim <twhitc...@gmail.com>
Date: Tue, 28 Jul 2009 09:24:50 -0700 (PDT)
Local: Tues, Jul 28 2009 12:24 pm
Subject: Re: How do I interface with a Django model containing a "model" field?
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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jespern  
View profile  
 More options Jul 30 2009, 3:44 am
From: jespern <jno...@gmail.com>
Date: Thu, 30 Jul 2009 00:44:32 -0700 (PDT)
Local: Thurs, Jul 30 2009 3:44 am
Subject: Re: How do I interface with a Django model containing a "model" field?
On Jul 28, 7:24 pm, Tim <twhitc...@gmail.com> wrote:

> 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?

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »