Answer the question
In order to leave comments, you need to log in
Created an api using django. In it, I use a ModelViewSet for the Project model. How to make delete and patch method work?
Created an api using django. In it, I use a ModelViewSet for the Project model. I can't get the delete and patch methods to work.
my model code
class Project(models.Model):
title = models.CharField('Имя', max_length=200)
contract_number = models.CharField('Номер договора', max_length=200)
description = models.TextField()
create_date = models.DateTimeField("date published",
auto_now_add=True)
author = models.ForeignKey(User, on_delete=models.PROTECT,
related_name="projects")
customer = models.ForeignKey('Customer',
on_delete=models.PROTECT,
related_name="projects")
price = models.FloatField('Стоимость проекта')
additional_pay = models.FloatField('Дополнительная оплата', blank=True,
null=True)
class Meta:
ordering = ["-pk"]
def __str__(self):
return self.title[:15]
class ProjectSerializer(serializers.ModelSerializer):
class Meta:
fields = ('title', 'contract_number', 'description', 'customer',
'price', 'additional_pay', 'pk')
model = Project
class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
def perform_create(self, serializer):
if serializer.is_valid:
serializer.save(author=self.request.user)
router = DefaultRouter()
router.register(r'projects', ProjectViewSet, basename='projects')
router.register(r'projects_detail', UpdateDeleteViewSet, basename='projects_detail')
router.register(r'customers', CustomerViewSet, basename='customers')
router.register(r'banks', BankViewSet, basename='banks')
router.register(r'checks', CheckViewSet, basename='checks')
urlpatterns = [
path('', include(router.urls))
]
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question