0

I am working on a project where I originally intended some fields to be required that are now optional. However, the newer fields will still throw an error if they are left blank.

From my models.py:

class PlanOfAction(models.Model):
PoA_1 = models.CharField(max_length=255, blank=True, default="")
q1_why = models.CharField(max_length=255, blank=True, default="")
PoA_2 = models.CharField(max_length=255, blank=True, default="yeah")
q2_why = models.CharField(max_length=255, blank=True, default="")
PoA_3 = models.CharField(max_length=255, blank=True, default="")
q3_why = models.CharField(max_length=255, blank=True, default="")

long_term_goal = models.CharField(max_length=255, blank=True, default="")
long_term_date = models.CharField(max_length=255, blank=True, default="")
long_term_actions_1 = models.CharField(max_length=255, blank=True, default="")
long_term_actions_2 = models.CharField(max_length=255, blank=True, default="")
long_term_actions_3 = models.CharField(max_length=255, blank=True, default="")

short_term_1 = models.CharField(max_length=255, blank=True, default="")
short_term_1_date = models.CharField(max_length=255, blank=True, default="")
short_term_actions_1_1 = models.CharField(max_length=255, blank=True, default="")
short_term_actions_1_2 = models.CharField(max_length=255, blank=True, default="")
short_term_actions_1_3 = models.CharField(max_length=255, blank=True, default="")

created_by = models.ForeignKey(User, related_name="user_poa")

objects = PlanManager()

created_at = models.DateTimeField(auto_now_add = True)
updated_at = models.DateTimeField(auto_now = True)

And where it is writing to the database in models.py

 new_plan = PlanOfAction.objects.create(
            PoA_1 = POST['PoA_1'],
            q1_why = POST['q1_why'],
            PoA_2 = POST['PoA_2'],
            q2_why = POST['q2_why'],
            PoA_3 = POST['PoA_3'],
            q3_why = POST['q3_why'],

            long_term_goal = POST['long_term_goal'],
            long_term_date = POST['long_term_date'],
            long_term_actions_1 = POST['long_term_actions_1'],
            long_term_actions_2 = POST['long_term_actions_2'],
            long_term_actions_3 = POST['long_term_actions_3'],

            short_term_1 = POST['short_term_goal_1'],
            short_term_1_date = POST['short_term_1_date'],
            short_term_actions_1_1 = POST['short_term_actions_1_1'],
            short_term_actions_1_2 = POST['short_term_actions_1_2'],
            short_term_actions_1_3 = POST['short_term_actions_1_3'],

            created_by = User.objects.get(id=id),
        )    

If I leave the short term actions blank, I get no errors. I did add these in later, and initially set them with blank=True and default="". After updating the other fields I deleted the migrations folder, db.sqlite3, run migrate, makemigrations, then migrate again. I error every time on PoA_2 = POST['PoA_2'] if that field is blank.

MultiValueDictKeyError at /view_your_plan
"u'PoA_2'"

Any ideas?

Using Python 2.7.

Brad C
  • 13
  • 5
  • Well yes, if the field is empty then it won't be in the POST data. This is why you should use a form for this kind of thing. – Daniel Roseman Oct 05 '18 at 22:08
  • this also may help: https://stackoverflow.com/questions/5895588/django-multivaluedictkeyerror-error-how-do-i-deal-with-it – deR_Ed Oct 05 '18 at 22:10
  • @DanielRoseman but if the default is set to an empty string, shouldn't it be writing an empty string? I am guessing my models file isn't updating properly, since I don't have the issue if I leave the short term actions blank. And I am using a form for this. I'm not sure what you mean by that. – Brad C Oct 05 '18 at 22:15
  • Yes if you had a form class then that would work. But you *are not using a form*, you're accessing the raw post data. And the browser won't send data for a field that is empty, so the key isn't in the post dict. Once again, *use a form*. – Daniel Roseman Oct 05 '18 at 22:18
  • @DanielRoseman Thanks for clarifying. I am obviously a noob, I thought you meant an html form not a form class. I've never done that before, I've always built in validations through a manager class. I am doing some reading up on it now. Do you have any thoughts on why I am not getting any errors if I leave, say, short_term_actions_1_1 blank? – Brad C Oct 05 '18 at 22:34
  • What I do to separate the logic of a model and a form is set all fields to blank=True null=True and handle everything in my form and views. – devdob Oct 06 '18 at 13:27

0 Answers0