Issues using Angular with Django REST Framework di

2019-07-27 23:03发布

I'm making an app where a user can post 'feedback items' so I've got these tables:

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

feedback: feedback_title, feedback_description, feedback_date_created, feedback_last_modified, feedback_user_id, feedback_upvotes, feedback_downvotes

I've got the following controller in an my angular app:

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);
    })

And then in my template home.html I'm displaying feedback posts

{% 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 %}

The issue is displaying the author of the post. When I do {{post.feedback_user}} I successfully get the id of the user who posted the feedback item. But when I do {{post.feedback_user.first_name}} nothing appears. I was able to "link" the posts array in my anjularjs file to the feedback table in my database, but how can I access the user table to get the name of the author and not just the id?

I guess I should include the Feedback model as well:

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条回答
登录 后发表回答