无法将数据发送到服务器angularjs

时间:2014-02-21 09:08:06

标签: javascript python json django angularjs

我在使用%http.post发布新数据时收到错误消息。我在django后端创建了一个类别,我在该领域写作的状态就像我在django后端模型中的选项字段中所做的那样

{"error_message": "Cannot resolve keyword 'category' into field. Choices are: id, inventory, name, slug", "traceback": "Traceback (most recent call last):\n\n "}

我的django模型看起来像这样。

from django.db import models

class Inventory(models.Model):
    APPROVAL_CHOICES = (
        (u'Good condition', u'Good condition'),
        (u'Bad condition', u'Bad condition'),
        (u'Broken', u'Broken'),
    )
    name         = models.CharField(max_length=255,  help_text="Enter inventory name")
    slug         = models.SlugField(unique=True, max_length=255)
    description  = models.CharField(max_length=255,  help_text="Enter inventory description")
    count        = models.IntegerField(max_length= 255, help_text="Enter count of Inventory", default=1)
    category     = models.ForeignKey('Category',  help_text="Enter category")
    location     = models.CharField(max_length=255, help_text="Enter inventory location")
    status       = models.CharField(max_length=20, choices=APPROVAL_CHOICES,  help_text="Enter inventory statuss")
    published    = models.BooleanField(default=True)
    created      = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return u'%s' % self.name


class Category(models.Model):
    name         = models.CharField(max_length=255,  help_text="Category name")
    slug         = models.SlugField(unique=True, max_length=255)

    def __unicode__(self):
        return u'%s' % self.name

使用angularjs我试图使用tastypie api

向服务器发布请求
$scope.createInventory = function(){
        var data = {"name":$scope.newinfo, "description":$scope.newinfo,"count":$scope.newinfo, "location":$scope.newinfo, "category":$scope.newinfo, "status":$scope.newinfo};
        $http.post("http://api.bos.lv/api/v1/inventory/?format=json", data).success(function (data, status, headers) {
            alert("Inventory Added!");
                $http.get(headers("location")).success(function(data){
                    $scope.info.push(data);
                });
        });
    };

然后我正在使用此表格

<td><input type="text" ng-model="newinfo.name"></td>
    <td><input type="text" ng-model="newinfo.slug"></td>
    <td><input type="text" ng-model="newinfo.description"></td>
    <td><input type="text" ng-model="newinfo.location"></td>
    <!--<td><input type="text" ng-model="newinfo.count"></td>--->
     <td><select ng-model="newinfo.category" ng-options="inventory.category.name for inventory in info.objects"></select></td>
    <td><input type="text" ng-model="newinfo.status"></td>
    <td><button ng-click="createInventory()">Saglabāt</button></td>

1 个答案:

答案 0 :(得分:1)

您必须指定一个类别。你得到一个错误,因为django需要类别的数据。您可以在django default=null中指定,因此您无法指定任何类别。