user3305175
user3305175

Reputation: 55

failed to sen data to server angularjs

i am getting an error while posting new data using %http.post . I have made a category at backend with django, and status i am writing in the field like i got in option fields in django backend model

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

my django model look like this.

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

Using angularjs i am trying to post a request to the server using 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);
                });
        });
    };

Then i am using this form

<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>

Upvotes: 0

Views: 89

Answers (1)

Eriks
Eriks

Reputation: 306

You have to specify a category. You get an error, because django needs data for category. You can specify in django, default=null so you could not specify any category.

Upvotes: 1

Related Questions