在我的django应用程序中:
models.py:
class Destination(models.Model):
name=models.CharField(max_length=30)
class Ride(models.Model):
driver = models.ForeignKey('auth.User', related_name='rides_as_driver')
destination=models.ForeignKey(Destination, related_name='rides_as_final_destination')
leaving_time=models.TimeField()
num_of_spots=models.IntegerField()
passengers=models.ManyToManyField('auth.User', related_name="rides_as_passenger")
mid_destinations=models.ManyToManyField(Destination, related_name='rides_as_middle_destination')
串行器:
class RideSerializer(serializers.ModelSerializer):
driver = serializers.ReadOnlyField(source='driver.username')
class Meta:
model = Ride
fields = ('id', 'driver', 'destination', 'leaving_time',
'num_of_spots', 'passengers', 'mid_destinations')
read_only_fields = ('id', 'driver', 'passengers', 'mid_destinations')
如您所见, mid_destinations 是一个ManyToMany字段。
我的问题是 - 如何发布到ManyToMany字段?
到常规字段我可以用这样的json POST,来自我的 android app :
{ "destination" : "LA", "num_of_spots" : "3", "leaving_time" : "14:35"}
等。
如何POST到ManyToMany字段?
提前致谢!
答案 0 :(得分:0)
两个步骤:
1.从PLATFORM
列表中删除'mid_destinations'
2.发送read_only_fields
我假设您使用的是DRF 3.