I have a project where I want to give users the ability to update some of the fields on an existing Case they have. I have set up the form and it appears to work. However, when I go to save the new, updated date, the .save() method doesn't work as expected. I do the x = case.save() command, and when I print out "x", I see the expected data that I input in the form. However, immediately after the if statement, it does not persist. The database does not update. Thoughts?
relevant methods in views.py:
@login_required(login_url='/accounts/login')
def stopThrough(request):
if request.method == 'POST':
c = IDForm(request.POST)
if c.is_valid():
val = c.cleaned_data['id']
urlStr = ("/polls/case/edit/" val)
return redirect(urlStr)
cForm = IDForm
return render(request, 'caseEdit.html', {'start': True, 'cForm' : cForm })
@login_required(login_url='/accounts/login')
def caseEdit(request, id):
if request.method == 'POST':
check = CaseForm(request.POST)
print(check)
if check.is_valid():
wtf = check.save()
return redirect("/polls/case/edit/0")
c = Case.objects.get(victimID=id)
cForm = CaseForm(instance=c)
return render(request, 'caseEdit.html', {'start': False, 'cForm' : cForm })
Case model
class Case(models.Model):
created_date = models.DateField(auto_now_add=True)
creator = models.ForeignKey(Organization, on_delete=models.CASCADE, related_name='creator', null=True)
victimID = models.CharField(max_length=100)
#service list functions
client_intake = models.BooleanField()
client_orientation = models.BooleanField()
criminal_justice_system_based_advocacy = models.BooleanField()
crisis_intervention_or_24_hour_hotline = models.BooleanField()
dental = models.BooleanField()
education = models.BooleanField()
emotional_moral_support = models.BooleanField()
employment_assistance = models.BooleanField()
family_reunification = models.BooleanField()
financial_assistance = models.BooleanField()
housing_shelter_advocacy = models.BooleanField()
house_rental_assistance = models.BooleanField()
interpreter_translator = models.BooleanField()
legal_services_general_consultation = models.BooleanField()
legal_services_immigration = models.BooleanField()
legal_services_family_law_services = models.BooleanField()
legal_services_victims_rights_laws = models.BooleanField()
legal_services_expunging_or_sealing_criminal_records = models.BooleanField()
legal_services_employment_wage = models.BooleanField()
legal_services_public_benefits_law = models.BooleanField()
medical_case = models.BooleanField()
mental_health_treatment = models.BooleanField()
ongoing_case_management = models.BooleanField()
personal_items = models.BooleanField()
protection_safety_planning = models.BooleanField()
repatriation = models.BooleanField()
social_service_advocacy = models.BooleanField()
substance_abuse_treatment = models.BooleanField()
transportation = models.BooleanField()
other = models.BooleanField()
relevant forms.py
class IDForm(forms.Form):
id = forms.CharField(label='SurvivorID', max_length=100)
class CaseForm(ModelForm):
class Meta:
model = Case
fields = ['creator','victimID','client_intake','client_orientation','criminal_justice_system_based_advocacy','crisis_intervention_or_24_hour_hotline',
'dental', 'education','emotional_moral_support', 'employment_assistance', 'family_reunification', 'financial_assistance',
'housing_shelter_advocacy', 'house_rental_assistance', 'interpreter_translator', 'legal_services_general_consultation', 'legal_services_immigration',
'legal_services_family_law_services', 'legal_services_victims_rights_laws',
'legal_services_expunging_or_sealing_criminal_records', 'legal_services_employment_wage', 'legal_services_public_benefits_law', 'medical_case',
'mental_health_treatment', 'ongoing_case_management', 'personal_items', 'protection_safety_planning', 'repatriation',
'social_service_advocacy', 'substance_abuse_treatment', 'transportation','other']
I have tried using an UpdateView instead of manually updating. It produced the same results. It appeared to work, but did not actually update my database.
views.py
class caseedit(UpdateView):
model = Case
fields = ['creator','victimID','client_intake','client_orientation','criminal_justice_system_based_advocacy','crisis_intervention_or_24_hour_hotline',
'dental', 'education','emotional_moral_support', 'employment_assistance', 'family_reunification', 'financial_assistance',
'housing_shelter_advocacy', 'house_rental_assistance', 'interpreter_translator', 'legal_services_general_consultation', 'legal_services_immigration',
'legal_services_family_law_services', 'legal_services_victims_rights_laws',
'legal_services_expunging_or_sealing_criminal_records', 'legal_services_employment_wage', 'legal_services_public_benefits_law', 'medical_case',
'mental_health_treatment', 'ongoing_case_management', 'personal_items', 'protection_safety_planning', 'repatriation',
'social_service_advocacy', 'substance_abuse_treatment', 'transportation','other']
success_url = "/polls/case/edit/0"
CodePudding user response:
- In your
stopThrough
method I didn't see any save method after validating the form - In
caseEdit
pass the instance incheck = CaseForm(request.POST)
so it will becheck = CaseForm(request.POST, instance=c)
, otherwise your are creating new objects instead of editing
CodePudding user response:
Solved... an obscure method referenced elsewhere had one singular line indented wrong and was returning something incorrect in the pipeline. If anyone else ever has this issue... I recommend looking EVERYWHERE the code goes. Even the stupid things.