diff options
| author | Tom Christie | 2011-06-02 15:22:14 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-06-02 15:22:14 +0100 |
| commit | bf9ea978bca8928ba5726e4ec3d76e81d72aada8 (patch) | |
| tree | a752b60913ed733f76eb78cca983d249f47de702 /djangorestframework/resources.py | |
| parent | f78076b5ba916aea24a8cbfd2fc9c7830c7d6c5e (diff) | |
| download | django-rest-framework-bf9ea978bca8928ba5726e4ec3d76e81d72aada8.tar.bz2 | |
Updating docs for 0.2
Diffstat (limited to 'djangorestframework/resources.py')
| -rw-r--r-- | djangorestframework/resources.py | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/djangorestframework/resources.py b/djangorestframework/resources.py index adf5c1c3..36bad914 100644 --- a/djangorestframework/resources.py +++ b/djangorestframework/resources.py @@ -44,7 +44,7 @@ def _model_to_dict(instance, resource=None): include = resource and resource.include or () exclude = resource and resource.exclude or () - extra_fields = fields and list(resource.fields) or [] + extra_fields = fields and list(fields) or list(include) # Model fields for f in opts.fields + opts.many_to_many: @@ -62,21 +62,26 @@ def _model_to_dict(instance, resource=None): # Method fields for fname in extra_fields: - if hasattr(resource, fname): - # check the resource first, to allow it to override fields - obj = getattr(resource, fname) - # if it's a method like foo(self, instance), then call it - if inspect.ismethod(obj) and len(inspect.getargspec(obj)[0]) == 2: - obj = obj(instance) - elif hasattr(instance, fname): - # now check the object instance - obj = getattr(instance, fname) - else: - continue + try: + if hasattr(resource, fname): + # check the resource first, to allow it to override fields + obj = getattr(resource, fname) + # if it's a method like foo(self, instance), then call it + if inspect.ismethod(obj) and len(inspect.getargspec(obj)[0]) == 2: + obj = obj(instance) + elif hasattr(instance, fname): + # now check the object instance + obj = getattr(instance, fname) + else: + continue + + # TODO: It would be nicer if this didn't recurse here. + # Let's keep _model_to_dict flat, and _object_to_data recursive. + data[fname] = _object_to_data(obj) - # TODO: It would be nicer if this didn't recurse here. - # Let's keep _model_to_dict flat, and _object_to_data recursive. - data[fname] = _object_to_data(obj) + except NoReverseMatch: + # Ug, bit of a hack for now + pass return data @@ -223,7 +228,7 @@ class FormResource(Resource): # In addition to regular validation we also ensure no additional fields are being passed in... unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set) - unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept')) # TODO: Ugh. + unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh. # Check using both regular validation, and our stricter no additional fields rule if bound_form.is_valid() and not unknown_fields: @@ -437,6 +442,9 @@ class ModelResource(FormResource): This method can be overridden if you need to set the resource url reversing explicitly. """ + if not hasattr(self, 'view_callable'): + raise NoReverseMatch + # dis does teh magicks... urlconf = get_urlconf() resolver = get_resolver(urlconf) |
