Dhanushka Amarakoon
Dhanushka Amarakoon

Reputation: 3762

Tastypie POST Does not FAIL

So I created a simple model as follows

class Titles(models.Model):
    titleID = models.CharField(max_length=20,primary_key=True)
    title = models.CharField(max_length=100)
    class Meta:
        verbose_name = "Titles"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.title  

Exposed it as a API as

class TitlesResource(AT.MultipartResource,AT.WrapView,ModelResource):
    class Meta:
        queryset = coreModels.Titles.objects.all()
        authentication = AT.cxenseAMSAPIAuthentication()
        authorization=Authorization()
        resource_name = 'titles'
        allowed_methods = ['get','post','put','patch']
        include_resource_uri=False
        limit=1000 

When I try to create a new object it works but if I mess up any of the fields it still works

eg:

http://localhost:8000/core/titles/

    {
      "I_am_not_suppling_a_correct_feild": "2",
      "title_not": "dept 1"
    }

[27/Oct/2017 10:54:12] DEBUG [django.db.backends:90] (0.001) UPDATE "core_titles" SET "title" = '' WHERE "core_titles"."titleID" = ''; args=('', '')

Shouldnt this fail as I am not supplying the needed fields?

Upvotes: 0

Views: 25

Answers (1)

Ashish S. Srivastava
Ashish S. Srivastava

Reputation: 11

When I try to create a new object it works but if I mess up any of the fields it still works

post data can have N no. of fields. It depends how you are handling each one of them.

Shouldn't this fail as I am not supplying the needed fields?

No. When a POST request is made for a ModelResource it is routed to obj_update and then to obj_create unless it is overridden. There it takes value from **kwargs and creates a model entry. In your case it's not there thus taking empty strings.

For reference, look at the documentation: https://django-tastypie.readthedocs.io/en/latest/non_orm_data_sources.html

Upvotes: 1

Related Questions