Hello Iam new in Django rest framework,i want to create custom delete funtion to delete .I successfully created create and update as in my below code but i cant create delete function.But i searched lots of article about it but i havent found .Hopefully U will help me .
class User(AbstractUser, BaseModel):
full_name = models.CharField(max_length=64)
addresss=models.CharField(max_length=40)
phoneno=models.IntegerField(null=True, blank=True)
email=models.EmailField()
password=models.CharField(max_length=40)
re_password=models.CharField(max_length=40)
gender=models.IntegerField(choices=gender_choice,default='0')
##models for student register
class Student(BaseModel):
user = models.ForeignKey(User, on_delete=models.CASCADE)
father_name=models.CharField(max_length=64)
mother_name=models.CharField(max_length=60)
date_of_birth=models.DateField(null=True)
def __str__(self):
return self.user.full_name
class StudentSerializer(serializers.HyperlinkedModelSerializer):
user = UserSerializer()
class Meta:
model=Student
fields=(
'id',
'father_name',
'mother_name',
'date_of_birth',
'user',
)
3. serializer.for user
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields=('full_name','id','addresss','phoneno','email','password','re_password','gender')
class StudentViewset(viewsets.ModelViewSet):
queryset = Student.objects.all()
serializer_class = StudentSerializer
http_methods = ['get', 'post','put',]
def create(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
row = serializer.data
user = row['user']
if user['password'] != user['re_password']:
return Response({'message':'pw not matched'})
email = username = row['user']['email']
user, created = User.objects.get_or_create(email=email,
defaults={'full_name':row['user']['full_name'],'addresss':row['user']['addresss'],
'phoneno':row['user']['phoneno'],'password':row['user']['password'] ,'re_password':row['user']['re_password'],
'gender':row['user']['gender'] ,'username':username})
if created == False:
return Response({'message':'Sorry the student is already registered.'})
student = Student.objects.create(user_id=user.id)
return Response(row)
def update(self,request,*args,**kwargs):
instance=self.get_object()
serializer=StudentUpdateSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
data = serializer.data
if data.get('father_name', False):
instance.father_name = data.get('father_name')
if data.get('mother_name', False):
instance.mother_name = data.get('mother_name')
instance.save()
return Response(serializer.data)
Q.
def delete(..)#How to write this delete Function???
ModelViewSet has a function which is called when you do DELETE request and it is called destroy
This is how it looks like:
def destroy(self, request, *args, **kwargs):
instance = self.get_object()
self.perform_destroy(instance)
return Response(status=status.HTTP_204_NO_CONTENT)
So to delete the instance from your DB - you can do
instance.delete()
that's what actually perform_destroy
does.
So you can take inspiration from this and implement your custom delete.
还有如通用删除类基于视图点击这里
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.