class EngrRegModel(models.Model):
engineer = models.OneToOneField(User, on_delete=models.CASCADE)
skills_description = models.TextField()
reg_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return f'Engineer {self.engineer.username}'
我创建了这个EngrRegModel,它与Django User Model一对一地具有关系,问题是,每当EngrRegModel实例被删除时,User也被删除,我希望它成为一种方式街头的实现,例如,当删除用户时,EngrRegModel的实例将被删除,但是当删除EngrRegModel的实例时,不应删除用户。我该如何解决?
@login_required
def engineerDeleteView(request):
engineer_instance = EngrRegModel.objects.all().filter(engineer =
request.user)[0].engineer
username = request.user
if engineer_instance == request.user and request.method == 'POST':
engineer_instance.delete()
messages.success(request, f'{username}, You Are No Longer A Sky
Squad Engineer')
return redirect('profile')
else:
print("NO")
messages.warning(
request, f'{username}, You Do Not Have Access To This!!!')
return redirect('profile')
这是处理删除功能的视图
答案 0 :(得分:0)
问题来自于已修复的视图:
@login_required
def engineerDeleteView(request):
engineer_instance = EngrRegModel.objects.all().filter(engineer =
request.user)[0]
username = request.user
if engineer_instance.engineer == request.user and request.method == 'POST':
engineer_instance.delete()
messages.success(request, f'{username}, You Are No Longer A Sky Squad
Engineer')
return redirect('profile')
else:
print("NO")
messages.warning(
request, f'{username}, You Do Not Have Access To This!!!')
return redirect('profile')