S
S
sttp2022-04-19 10:46:11
Django
sttp, 2022-04-19 10:46:11

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]

My serializer code
class ProjectSerializer(serializers.ModelSerializer):

    class Meta:
        fields = ('title', 'contract_number', 'description', 'customer',
                  'price', 'additional_pay', 'pk')
        model = Project

My viewset code
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)

My urls
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))
]

Please tell me how to make the delete and patch methods work. I couldn't do it myself with the help of Google.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Markin, 2022-04-19
@sttp

  • What request are you sending?
  • To which endpoint?
  • What is the response code?
  • Which mistakes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question