Commits

Martin Skala committed c38ac21

Introduce new mapping variables to define attributes that should be sent for create and update of a resources

- ROA_MODEL_CREATE_MAPPING is dictionary with model as a key and list of model's attributes as value we want to send when creating new model instance

- ROA_MODEL_UPDATE_MAPPING is dictionary with model as a key and list of model's attributes as value we want to send when updating particular model instance

See examples/django_roa_client/settings.py for example of notation

Comments (0)

Files changed (2)

django_roa/db/models.py

 
 logger = logging.getLogger("django_roa")
 
-ROA_MODEL_NAME_MAPPING = getattr(settings, 'ROA_MODEL_NAME_MAPPING', [])
 ROA_HEADERS = getattr(settings, 'ROA_HEADERS', {})
 ROA_FORMAT = getattr(settings, 'ROA_FORMAT', 'json')
 ROA_FILTERS = getattr(settings, 'ROA_FILTERS', {})
+ROA_MODEL_NAME_MAPPING = getattr(settings, 'ROA_MODEL_NAME_MAPPING', [])
+ROA_MODEL_CREATE_MAPPING = getattr(settings, 'ROA_MODEL_CREATE_MAPPING', {})
+ROA_MODEL_UPDATE_MAPPING = getattr(settings, 'ROA_MODEL_UPDATE_MAPPING', {})
 ROA_CUSTOM_ARGS = getattr(settings, "ROA_CUSTOM_ARGS", {})
 
 DEFAULT_CHARSET = getattr(settings, 'DEFAULT_CHARSET', 'utf-8')
         if origin and not getattr(meta, "auto_created", False):
             signals.pre_save.send(sender=origin, instance=self, raw=raw)
 
+        model_name = str(meta)
+
         # If we are in a raw save, save the object exactly as presented.
         # That means that we don't try to be smart about saving attributes
         # that might have come from the parent class - we just save the
                 payload = serializer().serialize_object(self)
             else:
                 payload = {}
-                for field in meta.local_fields:
+                local_fields = []
+                m2m_fields = []
+                attribute_map = ROA_MODEL_CREATE_MAPPING
+
+                if force_update or pk_is_set and not self.pk is None:
+                    attribute_map = ROA_MODEL_UPDATE_MAPPING
+
+                if attribute_map.has_key(model_name):
+                    for field in meta.local_fields:
+                        if field.attname in attribute_map.get(model_name):
+                            local_fields.append(field)
+                    for field in m2m_fields:
+                        if field.attname in attribute_map.get(model_name):
+                            m2m_fields.append(field)
+                else:
+                    local_fields = meta.local_fields
+                    m2m_fields = meta.many_to_many
+
+                for field in local_fields:
                     # Handle FK fields
                     if isinstance(field, models.ForeignKey):
                         field_attr = getattr(self, field.name)

examples/django_roa_client/settings.py

 ROA_ARGS_NAMES_MAPPING = {
     'ORDER_BY': 'order',
 }
+ROA_MODEL_CREATE_MAPPING = {
+    'package.somemodel': ['attribute1', 'attribute2', 'attribute3', 'attribute4'],
+}
+ROA_MODEL_UPDATE_MAPPING = {
+    'package.somemodel': ['attribute2', 'attribute3'],
+}
 # Enable HTTP authentication through django-piston
 from restkit import BasicAuth
 ROA_FILTERS = [ BasicAuth('django-roa', 'roa'), ]