所以我面临以下问题。
在Android Studio中创建整数列表时:
List<Integer> myIDs= new ArrayList<>();
我将整数(id&#39; s)按给定的顺序排列,结果是,p.e:
myIds = [69, 65, 67]
我使用 Retrofit 将请求发送到服务器:
@POST("looks/")
@FormUrlEncoded
Call<Look> createLook(
@Field("myIds") List<Integer> myIds,
...,
...);
mAPIService.createLook(myIDs, ..., ...).enqueue(new Callback<Look>() {
@Override
public void onResponse(Call<Look> call, Response<Look> response) {
if (response.isSuccessful()) {
Log.d("OK: ", "post submitted to API." + response.body().toString());
// We finish the Activity.
finish();
} else {
...
...
}
finish();
}
@Override
public void onFailure(Call<Look> call, Throwable t) {
}
});
问题在于,当我将List发送到我的django服务器时,它会自行订购它:
"myIds ": [
65,
67,
69
]
有关为什么会发生这种情况的任何想法,我该如何解决?
在服务器上,这是我的models.py
...
myIds = models.ManyToManyField('garments.Garment')
...
views.py
class LookList(generics.ListCreateAPIView):
serializer_class = LookSerializer
def get_queryset(self):
queryset = Look.objects.all()
username = self.request.query_params.get('username',None)
date = self.request.query_params.get('date', None)
if date is not None:
queryset = queryset.filter(date=date)
if username is not None:
queryset = queryset.filter(username=username)
return queryset
class LookDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = Look.objects.all()
serializer_class = LookSerializer
serializers.py
class LookSerializer(serializers.ModelSerializer):
class Meta:
model = Look
fields = ('id','garment_id','username','date')
先谢谢你了!