使用Angular和Django REST Framework显示来自SQL数据库

时间:2016-06-15 23:26:40

标签: angularjs django django-rest-framework

我正在创建一个应用,用户可以在其中发布反馈项目'所以我有这些表格:

user:id,password,last_login,is_superuser,username,first_name,last_name,email,is_staff,is_active,date_joined

反馈:feedback_title,feedback_description,feedback_date_created,feedback_last_modified,feedback_user_id,feedback_upvotes,feedback_downvotes

我的角度应用程序中有以下控制器:

app.controller('HomeController', ['$scope', '$http', function($scope, $http){
    $scope.deleteFeedback = function(array, index, id) {
        console.log('post id: ' + id);
        $http.delete('/api/feedbacks/' + id).success(function(data) {
            console.log('Post #' + id + ' Successfully Deleted');
            array.splice(index, 1);
        }).error(function(data) {
            console.log(data);
        });
    }
    $scope.showOptions = function(feedback_user) {
        if(feedback_user == $scope.userid)
            return true;
        return false;
    }
    $http.get('/checklogin').success(function(data) {
        console.log('Current User: ' + data.user_id);
        $scope.userid = data.user_id;
    }).error(function(data) {
        console.log('error: ' + data);
    });
    $scope.posts = [];
    $http.get('/api/feedbacks').success(function(result) {
        for(var i = 0; i < result.results.length; i++) {
            $scope.posts.push(result.results[i]);
            //console.log(result.results[i]);
        }
    }).error(function(data,status) {
        console.log(data);
        console.log(status);
    })

然后在我的模板home.html中显示反馈帖

{% verbatim %}
<h3>Home Base</h3>
    <div ng-repeat="post in posts">
        <h4> {{post.feedback_title}} by {{post.feedback_user}}</h4>
        <h5>Created {{post.feedback_date_created | date}} | Last Modified {{post.feedback_last_modified | date}}</h5>
        <p>{{ post.feedback_description }}</p>
        <button ng-if="showOptions(post.feedback_user)" ng-click="updateFeedback(posts, $index)" class="btn btn-primary" value="Edit">Edit</button>
        <button ng-if="showOptions(post.feedback_user)" ng-click="deleteFeedback(posts, $index, post.id)" class="btn btn-primary" value="Delete">Delete</button>
        <br>
    </div>
{% endverbatim %}

问题是显示帖子的作者。当我{{post.feedback_user}}时,我成功获得发布反馈项的用户的ID。但是当我{{post.feedback_user.first_name}}时,什么都没有出现。我能够&#34;链接&#34;我的anjularjs文件中的posts数组到我数据库中的反馈表,但是如何访问用户表来获取作者的名字而不仅仅是id?

我想我也应该包括反馈模型:

class Feedback(models.Model):
    id = models.AutoField(primary_key=True)
    feedback_title = models.CharField(max_length = 50)
    feedback_description = models.TextField()
    feedback_date_created = models.DateTimeField('date feedback created')
    feedback_last_modified = models.DateTimeField('date feedback last modified')
    feedback_user = models.ForeignKey(Profile, on_delete=models.CASCADE)
    feedback_project = models.ForeignKey(Project, on_delete=models.CASCADE)
    feedback_status = models.ForeignKey(Status, on_delete=models.CASCADE)
    feedback_upvotes = models.IntegerField(default=0)
    feedback_downvotes = models.IntegerField(default=0)

    def __str__(self):
        return "%s" % self.feedback_title

0 个答案:

没有答案