class DroneList(generics.ListCreateAPIView):
queryset = Drone.objects.all()
serializer_class = DroneSerializer
name = 'drone-list'
filter_fields = (
'name',
'drone_category',
'manufacturing_date',
'has_it_competed',
)
search_fields = (
'^name',
)
ordering_fields = (
'name',
'manufacturing_date',
)
permission_classes = (
permissions.IsAuthenticatedOrReadOnly,
custompermission.IsCurrentUserOwnerOrReadOnly,
)
def perform_create(self, serializer):
serializer.save(owner = self.request.user)
Views.py
class DroneSerializer(serializers.HyperlinkedModelSerializer):
# Display the category name
drone_category = serializers.SlugRelatedField(queryset=DroneCategory.objects.all(),
slug_field='name')
owner = serializers.ReadOnlyField(source='owner.username')
class Meta:
model = Drone
fields = (
'url',
'name',
'drone_category',
'manufacturing_date',
'has_it_competed',
'inserted_timestamp',
'owner'
)
class UserDroneSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Drone
fields = (
'url',
'name')
Serializer.py
class DroneCategory(models.Model):
name = models.CharField(max_length=250, unique=True)
class Meta:
ordering = ('name',)
def __str__(self):
return self.name
class Drone(models.Model):
name = models.CharField(max_length=250, unique=True)
drone_category = models.ForeignKey(
DroneCategory,
related_name='drones',
on_delete=models.CASCADE)
manufacturing_date = models.DateTimeField()
has_it_competed = models.BooleanField(default=False)
inserted_timestamp = models.DateTimeField(auto_now_add=True)
owner = models.ForeignKey(
'auth.User',
related_name='drones',
on_delete=models.CASCADE
)
class Meta:
ordering = ('name',)
def __str__(self):
return self.name
models.py
{"name": "Python Drone",
"drone_category": "Quadcopter", "manufacturing_date": "2017-07-
16T02: 03: 00.716312Z "," has_it_competed ":" false "}
Отправить запрос, который я отправил.
поля не читаются django, как показано в ответе:
"название": [
"Это поле обязательно к заполнению."
],
"drone_category": [
"Это поле обязательно к заполнению."
],
"Дата изготовления": [
"Это поле обязательно к заполнению."
]