dimanche 30 novembre 2014

How to update model using angularjs, Django,Django rest framework


Vote count:

0




I have such json representation of a post by its id:



{"title": "about me", "content": "I like program", "created": "2014-11-29T18:07:18.173Z", "rating": 1, "id": 1}


I try to update rating by button click:



<button ng-click="click(post.id)">Click me</button>


I have such javascript code:



<script>
var demoApp = angular.module('demoApp',['ngResource']);
demoApp.controller( 'AllPosts', function ($scope, $http)
{
$http.get('/blogpost/?format=json').success(function(data,status,headers,config)
{
$scope.posts = data.results;
$scope.predicate = '-title';

$scope.click = function(post_id, $resource){
var Post = $resource('/update/:PostId ',{PostId:post_id,salutation:'json'} );
post = Post.get({PostId:post_id}, function() {
post.rating = post.rating+ 1 ;
post.$save();
});
};

}).error(function(data,status,headers,config)
{} )
;})

</script>


Besides i have such view to have a json by certain post by its id:



class UpdateModel(generics.RetrieveUpdateDestroyAPIView):
lookup_field = 'id'
queryset = BlogPost.objects.all()
serializer_class = BlogPostSerializer
permission_classes = (AllowAny,)


asked 20 secs ago







How to update model using angularjs, Django,Django rest framework

Aucun commentaire:

Enregistrer un commentaire