簡體   English   中英

如何為以下 models.py 和 serializers.py 創建 REST 視圖集? 我是 django rest 框架的新手

[英]How to create REST viewset for following models.py and serializers.py? i am new with django rest framework

I created an REST API which generate coupon codes, but now i like to redeem/Validate coupon code using Postman, HTML, Python script so anyone can help me to create ViewSets for this models.py and serializers.py file, I am new with django 所以請稍微詳細解釋一下。 謝謝

這是我的 models.py 文件:-

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator


class Coupon(models.Model):
    code = models.CharField(max_length=50, unique=True)
    valid_from = models.DateTimeField()
    valid_to = models.DateTimeField()
    discount = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)])
    active = models.BooleanField()

    def __str__(self):
        return self.code

這是我的 serializers.py 文件

from rest_framework import serializers
from .models import Coupon


class CouponSerializer(serializers.ModelSerializer):
    class Meta:
        model = Coupon
        fields = '__all__'

另外,如果我的代碼中有任何錯誤,請告訴。

I created an REST API which generate coupon codes, but now i like to redeem/Validate coupon code using Postman, HTML, Python script so anyone can help me to create ViewSets for this models.py and serializers.py file, I am new with django 所以請稍微詳細解釋一下。 謝謝

這是我的 models.py 文件:-

from django.db import models
from django.core.validators import MinValueValidator, MaxValueValidator


class Coupon(models.Model):
    code = models.CharField(max_length=50, unique=True)
    valid_from = models.DateTimeField()
    valid_to = models.DateTimeField()
    discount = models.IntegerField(validators=[MinValueValidator(0), MaxValueValidator(100)])
    active = models.BooleanField()

    def __str__(self):
        return self.code

這是我的 serializers.py 文件

from rest_framework import serializers
from .models import Coupon


class CouponSerializer(serializers.ModelSerializer):
    class Meta:
        model = Coupon
        fields = '__all__'

另外,如果我的代碼中有任何錯誤,請告訴。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM