Exemplo n.º 1
0
    def update(self, instance, validated_data):
        raise_errors_on_nested_writes('update', self, validated_data)

        ModelClass = self.Meta.model
        relations = model_meta.get_relations_data(ModelClass, validated_data)

        # Generate filter for getting existing object once more. Earlier,
        # We generate query to the database, because we can get relationship
        # objects, which attached to the existing object. But can't use this
        # instance further. To avoid issues with it, just get object once more
        filter_args = (getattr(ModelClass, field) == getattr(instance, field)
                       for field in model_meta.model_pk(ModelClass))

        # Simply set each attribute on the instance, and then save it.
        # Note that unlike `.create()` we don't need to treat many-to-many
        # relationships as being a special case. During updates we already
        # have an instance pk for the relationships to be associated with
        session = settings.SQLALCHEMY_SESSION(expire_on_commit=False)
        try:
            instance = session.query(ModelClass).filter(*filter_args).first()
            if not instance:
                raise ValidationError("Object does not exist.")

            for field_name, value in validated_data.items():
                setattr(instance, field_name, value)

            # Update relation objects
            if relations:
                session.enable_relationship_loading(instance)
                for field_name, value in relations.items():
                    setattr(instance, field_name, [])
                    session.refresh(instance)

                    if value:
                        setattr(instance, field_name, value)

            session.commit()
        finally:
            session.close()

        return instance
Exemplo n.º 2
0
 def test_model_pk_from_model_with_composite_pk(self):
     self.assertEqual(
         set(model_pk(self.TestModelPkWithCompositePkModel)),
         {'id', 'username'}
     )
Exemplo n.º 3
0
 def test_model_pk_from_model_with_one_pk(self):
     self.assertEqual(
         set(model_pk(self.TestModelPkWithSinglePkModel)),
         {'id', }
     )
Exemplo n.º 4
0
 def test_model_pk_from_model_with_composite_pk(self):
     self.assertEqual(set(model_pk(self.TestModelPkWithCompositePkModel)),
                      {'id', 'username'})
Exemplo n.º 5
0
 def test_model_pk_from_model_with_one_pk(self):
     self.assertEqual(set(model_pk(self.TestModelPkWithSinglePkModel)), {
         'id',
     })
Exemplo n.º 6
0
 def _get_object_pk(self, obj):
     mapper = obj.__mapper__
     model = mapper.class_
     pk_fields = model_pk(model)
     data = {str(field): getattr(obj, field) for field in pk_fields}
     return data if len(pk_fields) > 1 else data[pk_fields[0]]
Exemplo n.º 7
0
 def _get_filter_args(self, query, data):
     mapper = query._bind_mapper()
     model = mapper.class_
     pk_fields = model_pk(model)
     return (getattr(model, field) == data[field] for field in pk_fields)
Exemplo n.º 8
0
 def _get_object_pk(self, obj):
     mapper = obj.__mapper__
     model = mapper.class_
     pk_fields = model_pk(model)
     data = {str(field): getattr(obj, field) for field in pk_fields}
     return data if len(pk_fields) > 1 else data[pk_fields[0]]
Exemplo n.º 9
0
 def _get_filter_args(self, query, data):
     mapper = query._bind_mapper()
     model = mapper.class_
     pk_fields = model_pk(model)
     return (getattr(model, field) == data[field] for field in pk_fields)