我已经将reactjs用于星级,并且已经使用了react-star-rating模块。我使用了django-rest-framework来设计api。我的代码是
class Rating(models.Model):
owner = models.ForeignKey(User)
recipe = models.ForeignKey(Recipe)
#stars
STAR_CONVERSION = (
(1, 'one'),
(2, 'two'),
(3, 'three'),
(4, 'four'),
(5, 'five'),
)
tasty = models.PositiveSmallIntegerField(choices=STAR_CONVERSION)
healthy = models.PositiveSmallIntegerField(choices=STAR_CONVERSION)
service = models.PositiveSmallIntegerField(choices=STAR_CONVERSION)
rating_comment = models.TextField(null=True, blank=True)
api.py(/ API /评价/)
class RatingViewSet(viewsets.ModelViewSet):
queryset = Rating.objects.all()
serializer_class = RatingSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
class RecipeRatingViewSet(generics.ListCreateAPIView):
serializer_class = RatingSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,)
def get_queryset(self, *args, **kwargs):
return Rating.objects.filter(recipe__id=self.kwargs.get('pk'))
前端(reactjs星级评定部分)
handleRatingClick(e,data){
console.log(e);
console.log(data.rating); # gives value like 1, 2, 3, 4, 5
console.log(data.name); gives value like tasty, healthy
$.ajax({
url:"/api/ratings/",
data:data.rating,
type:'POST',
success: function(data, textStatus, xhr ) {
console.log(data);
}
});
}
<div className="row">
<div className="col s12 m5">
<div className="card-panel teal">
<span className="white-text">
<StarRating name="tasty" caption="tasty" totalStars={5} onRatingClick={this.handleRatingClick.bind(this)} />
<StarRating name="healthy" caption="healthy" totalStars={5} onRatingClick={this.handleRatingClick.bind(this)} />
<StarRating name="service" caption="service" totalStars={5} onRatingClick={this.handleRatingClick.bind(this)} />
</span>
</div>
</div>
</div>
我收到此错误
**{recipe: ["This field is required."], owner: ["This field is required."],…}
healthy:["This field is required."]
owner:["This field is required."]
recipe:["This field is required."]
service:["This field is required."]
tasty:["This field is required."]**
如何将这些评级(美味,健康,服务)保存到api?