在一个请求中DRF多个资源的创建/删除/更新

时间:2019-06-06 11:57:32

标签: rest django-rest-framework

DRF中是否存在在REST API中创建,更新和/或删除多个资源的标准方法?

说路径using MongoDB.Driver; using MongoDB.Entities; using System.Collections.Generic; namespace StackOverflow { public class Program { public class Book : Entity { public string Title { get; set; } public List<City> CitiesInBook { get; set; } = new List<City>(); public SearchResult SearchResult { get; set; } } public class City { public string Name { get; set; } public Coordinates2D Location { get; set; } } public class SearchResult { public Coordinates2D Location { get; set; } public double DistanceKM { get; set; } } static void Main(string[] args) { //connect to mongodb new DB("test"); //create a geo2dsphere index with key "CitiesInBook.Location" DB.Index<Book>() .Key(x => x.CitiesInBook[-1].Location, KeyType.Geo2DSphere) .Create(); //create 3 locations var paris = new City { Name = "paris", Location = new Coordinates2D(48.8539241, 2.2913515) }; var versailles = new City { Name = "versailles", Location = new Coordinates2D(48.796964, 2.137456) }; var poissy = new City { Name = "poissy", Location = new Coordinates2D(48.928860, 2.046889) }; //create a book and add two cities to it var book = new Book { Title = "the power of now" }; book.CitiesInBook.Add(paris); book.CitiesInBook.Add(poissy); book.Save(); var eiffelTower = new Coordinates2D(48.857908, 2.295243); //find all books that have places within 20km of eiffel tower. var books = DB.GeoNear<Book>( NearCoordinates: eiffelTower, DistanceField: b => b.SearchResult.DistanceKM, IncludeLocations: b => b.SearchResult.Location, MaxDistance: 20000) .ToList(); } } } 是指待办事项列表的集合,而/todo-lists是指定待办事项列表的项目的集合。例如GET /todo-lists/<id>/items返回

/todo-lists/<id>/items

假设用户获取此数据并按如下方式在本地进行编辑(例如,在Web浏览器或移动应用上)

[
    {"id": 1, "text": "buy groceries"},
    {"id": 2, "text": "do laundry"},
    {"id": 4, "text": "book flight"},
    {"id": 7, "text": "walk the dog"}
]

然后应将哪种请求发送回服务器以应用此更改?每次创建/更新/删除一项时,都可以发送单独的HTTP请求,但这显然效率不高。另外,我不确定列表的一致性,因为新创建的对象未指定ID,而现有对象则指定ID。

我已经看过这个post和其他内容,但是并不能完全回答我的问题。在许多应用程序中,这种操作通常应该是一个普遍的问题(DRF之外)。

0 个答案:

没有答案