简体   繁体   中英

How do I create RESTful APIs in Django without using third party app like DRF,Tastypie

for example I have two models

class User(models.Model):
    username = models.CharField(max_length=100,unique=True)
    password = models.CharField(max_length=100,null=False,blank=False)

class Car(models.Model):
    user = models.ForeignKey(User)
    car_name = models.CharField(max_length=100,null=True,blank=True)
    car_price = models.CharField(max_length=100,null=True,blank=True)

each user can have multiple cars.

I would like to add car name and price by making post request something like

curl -X POST -d "car_name=BMW&car_price=$0.5M" -u username:userpasswd

There is currently no other way that using a third party app to create a REST API in Django. This is something not natively supported in Django.

But Tastypie and Django Rest Framework are two very good third apps, there's nothing wrong to use them. They both have good documentation and there exists lots of posts talking about them.

Once you set your REST API using one of these apps, you can use CURL (like in your example) or Python requests to make post/get/.. requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM