def post(self, request): serializer = SchoolSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, school_id): serializer = SchoolSerializer(get_object(School, school_id), data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def add(self, request): if request.user.is_staff != 1: return Response({"error_code": 2}) sc=SchoolSerializer(data=json.loads(request.body.decode()),many=True) print(json.loads(request.body.decode())) print(request.body) if sc.is_valid(): print(sc.validated_data) sc.save() return Response({ "error_code": 0, "data": sc.validated_data }) else: return Response({"error_code": 1})
class UniversitySchoolSerializer(serializers.ModelSerializer): school = SchoolSerializer() class Meta: model = UniversitySchools fields = ('school', 'is_main_school') @transaction.atomic def create(self, validated_data): university_id = self.context.get('university_id') main_school = validated_data.pop('is_main_school') if main_school: UniversitySchools.objects.filter( university=university_id, is_main_school=True).update(is_main_school=False) school = School.objects.create(**validated_data['school']) unv_sch = UniversitySchools.objects.create(university_id=university_id, school=school, is_main_school=main_school) return unv_sch @transaction.atomic def update(self, instance, validated_data): School.objects.filter(pk=instance.school.pk).update( **validated_data['school']) instance.is_main_school = validated_data.get( 'is_main_school', instance.is_main_school) instance.save() return instance
def getAll(self, request): schoolCode=request.GET.get('schoolCode') if schoolCode is not None: sc=School.objects.filter(schoolCode=schoolCode) else: sc=School.objects.all() return Response({ "error_code": 0, "data": SchoolSerializer(sc,many=True).data })
def get(self, request): code=request.GET.get('code') try: sc=School.objects.get(code=code) except BaseException: return Response({"error_code": 1}) return Response({ "error_code": 0, "data": SchoolSerializer(sc).data })
class OutletSerializer(serializers.ModelSerializer): from school.serializers import SchoolSerializer school = SchoolSerializer(many=False) class Meta: from .models import Outlet model = Outlet fields = ("id", "school", "short_name") read_only_fields = fields
def get_all_school(request): try: # 获取所有 user 数据 schools = School.objects.all() serializer = SchoolSerializer(schools, many=True) res = serializer.data # res = { # "code": 200, # "data": serializer.data # } except Exception as e: res = {"code": 0, "errMsg": e} return HttpResponse(json.dumps(res), content_type="application/json")
def search_school_by_name_like(request): try: data = json.loads(request.body) # 获取符合条件的 user 数据 schools = School.objects.filter( Q(name__icontains=data.__getitem__('keywords'))) # schools = School.objects.filter(name=data.__getitem__('keywords')) serializer = SchoolSerializer(schools, many=True) res = serializer.data # res = { # "code": 200, # "data": serializer.data # } except Exception as e: res = {"code": 0, "errMsg": e} return HttpResponse(json.dumps(res), content_type="application/json")
class StudentSerializer(serializers.ModelSerializer): clazz = ClassSerializer() school = SchoolSerializer(source='clazz.school') mobile = serializers.CharField(source='device.mobile') class Meta: model = Student fields = ( 'student_id', 'name', 'gender', 'age', 'clazz', 'school', 'mobile', ) read_only_fields = fields
def get(self, request, school_id): return Response(SchoolSerializer(get_object(School, school_id)).data)
def get(self, request): schools = School.objects.all().order_by('name') serializer = SchoolSerializer(schools, many=True) return Response(serializer.data)
def list_schools(request): if request.method == 'GET': schools = School.objects.all() serializer = SchoolSerializer(schools, many=True) return JSONResponse(serializer.data)
def get_school(request, uuid): if request.method == 'GET': school = School.objects.get(uid=uuid) serializer = SchoolSerializer(school) return JSONResponse(serializer.data)