在React和Django中接收单选按钮值

时间:2019-07-31 12:43:57

标签: django reactjs django-rest-framework django-serializer

我的应用程序中有两种类型的用户,即供应商和教师,以及我的React中的注册表单,其中包含单选按钮以选择其中一个,但是我无法将单选按钮的值保存在django ORM中

这是代码:

SignUpForm.js

import React from 'react';
import PropTypes from 'prop-types';

class SignupForm extends React.Component {
  state = {
    username: '',
    password: '',
    type: '',
  };

  handle_change = e => {
    const name = e.target.name;
    const value = e.target.value;
    this.setState(prevstate => {
      const newState = { ...prevstate };
      newState[name] = value;
      console.log(newState);
      return newState;

    });
  };

  render() {
    return (
      <form onSubmit={e => this.props.handle_signup(e, this.state)}>
        <h4>Sign Up</h4>
        <label htmlFor="username">Username</label>
        <input
          type="text"
          name="username"
          value={this.state.username}
          onChange={this.handle_change}
        />
        <label htmlFor="password">Password</label>
        <input
          type="password"
          name="password"
          value={this.state.password}
          onChange={this.handle_change}
        />
        <label htmlFor="type">User type</label>
        <input
          type="radio"
          name="type"
          value="1"
          checked={this.state.type == "1"}
          onChange={this.handle_change}
        />Shipper
        <input
          type="radio"
          name="type"
          value="0"
          checked={this.state.type == "0"}
          onChange={this.handle_change}
        />Supplier
        <input type="submit" />
      </form>
    );
  }
}

export default SignupForm;

SignupForm.propTypes = {
  handle_signup: PropTypes.func.isRequired
};

Serializers.py

from rest_framework import serializers
from .models import Quiz
from rest_framework import serializers
from rest_framework_jwt.settings import api_settings
from .models import User



class TodoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Quiz
        fields = ('id', 'origin', 'destination', 'scheduled_date')


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username',)


class UserSerializerWithToken(serializers.ModelSerializer):
    token = serializers.SerializerMethodField()
    password = serializers.CharField(write_only=True)

    def get_token (self, obj):
        jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
        jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER

        payload = jwt_payload_handler(obj)
        token = jwt_encode_handler(payload)
        return token

    def create (self, validated_data):
        password = validated_data.pop('password', None)
        instance = self.Meta.model(**validated_data)

        if password is not None:
            instance.set_password(password)
        instance.save()
        return instance

    class Meta:
        model = User
        fields = ('token', 'username', 'password')

Models.py

class User(AbstractUser):
    is_supplier = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False)
    email = models.EmailField(default=False)

我尝试在fields = ('token', 'username', 'password')中添加字段“ is_teacher”,但是它不起作用。

要在用户模型中保存单选按钮值,我需要做哪些更改?

0 个答案:

没有答案