Hi everyone,

I have a model which has unpredictable field names. I am using Django-
ROA to retrieve records of this model remotely, so I lose some
flexibility in terms of how I can query the data:

node = Node.objects.get(id=node_id)
[{
  "pk": "1",
  "model": "pages.Node",
  "fields":{
    "id": "1",
    "child_count": "3",
    "template_id": "1",
    "active": "0",
    "title": "Home",
    "created_at": "2010-05-06 19:15:33",
    "updated_at": "2010-05-06 19:15:33",
    "lft": "1",
    "rgt": "12",
    "level": "0",
    "version": "7",
    "node_id": "1",
    "field_body": "Sample Body"
  }
}]

On the remote server, the matching record is joined with a table
representing a dynamic content type (variable fields), creating the
full node record. Here is the Node model:

class Node(django_roa.Model):
  created_at = models.DateTimeField()
  updated_at = models.DateTimeField()
  template_id = models.IntegerField()
  child_count = models.IntegerField()
  parent_id = models.IntegerField()
  active = models.BooleanField()
  title = models.CharField(max_length=200)
  rgt = models.IntegerField()
  lft = models.IntegerField()
  level = models.IntegerField()
  version = models.IntegerField()

I'm having a big problem with this, because Django's deserializer uses
models to vivify the JSON, and there will always be fields in the
returned object that are not defined in Node. In this sample packet,
"field_body" is a text field.

This isn't all in the air however. I can readily determine a given
node's additional fields by querying the server for fields in the
node's template:

Field.objects.all().filter(template_id=1)
[{
  "pk": "1",
  "model": "pages.Field",
  "fields": {
    "id": "1",
    "type": "longtext",
    "name": "body",
    "label": "Body",
    "required":null,
    "options":null,
    "row_id": "1",
    "created_at": "2010-05-06 19:15:32",
    "updated_at": "2010-05-06 19:15:32"
  }
}]

So based on that data, I presume could make Django aware, somehow,
that this model has an additional longtext "body" field, stored in the
database as "field_body." I just don't know what to do with this data.
I feel like I have a chicken-and-egg problem here: I cannot parse
returned JSON without a compliant model, and I cannot build that model
in memory without a returned JSON packet identifying the additional
fields.

That makes me think that I will separate remote queries to make all of
this happen:

1. Get the base node data and include field data in the response
2. Build a new model and request the full model instance

Altogether, I expect that to get pretty expensive in this application,
but it will work as a last resort. I have two other ideas that I find
more appealing:

1. Include field metadata in the remote JSON response, and build a
model that can handle this data during the Django request.
2. Define a "dynamic" field allowing Django to access "field_*", with
this field always being a CharField so that it can accept any JSON
data. Perform type-specific casting and operations where necessary,
explicitly.

What are my other options? Has anyone set up something like this
before?

Thanks,
Thomas

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to