How to get all objects in a Django model(Databse table) based on a filter using AngularJs
<p>Hi I have a Django model as below:</p>

<pre><code>from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
# Create your models here.

class Journal(models.Model):
    name = models.CharField(max_length=120)
    created_by = models.ForeignKey(User, related_name='+')
    date_created = models.DateTimeField(auto_now=False, auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True, auto_now_add=False )

    def __unicode__(self):
        return (self.name)
</code></pre>

<p>And my angularJs below:</p>

<pre><code>var app = angular.module("journalApp", []);
    app.controller("myCtrl", function($scope) {

});
</code></pre>

<p>How can I make a query in AngularJs so that i get all the objects from my Django model based on the user who logged in? Any idea guys? Thanks in advance.</p>

<p>Try the this method on server side</p>

<pre><code>import json
from django.http import JsonResponse
from django.views.generic View

from .models import Journal


class JSONResponseMixin(object):
    """
    A mixin that can be used to render a JSON response.
    """
    def render_to_json_response(self, context, **response_kwargs):
         """
        Returns a JSON response, transforming 'context' to make the payload.
        """
        return JsonResponse(
        self.get_data(context),
        **response_kwargs
        )

    def get_data(self, context):
        """
        Returns an object that will be serialized as JSON by json.dumps().
        """
        return context

class JournalView(JSONResponseMixin, View):

    def get(self, request, *args, **kwargs):
        response_data = {}
        response_data['data'] = Journal.objects.filter(created_by=self.request.user).values()
        return self.render_to_json_response(dict(response=response_data))
</code></pre>

<p>Of course you need to add the view it to your urls.py</p>

<p>Client side try this method</p>

<pre><code>var app = angular.module("journalApp", []);
    app.controller("myCtrl", function($scope, $http) {

    var url = "/your-url-to-view"; # {% url 'init_data' %}}

    $scope.initData = function(){
        $http.get(url).then(function(data){
            $scope.data = data.data.response;
        })
    }

});
</code></pre>

python angularjs django