[Fixed]-Django – forms.Form can't save current user and form is valid but not saved

1👍

I see two practical solutions to your problem:

  1. Since the field ‘owner’ is new to the model, you will have to add a default id in the migration to put that in the column to the already existent data.
  2. You could set ‘blank=null’ in the ‘owner’ field, in that way django it will not force the data in that column.

You said in the comments that before the command ‘makemigrations’ didn’t ask you for anything and now it does, this is because now you have records in that table and if you add a new column, neither django or postgres know what to put in that column in the old records, perhaps you could ‘flush’ your database? Of course, only if it is a dev database.

0👍

After some more reading and experimenting I found some additional points to add for future readers of the question.

The first part of the error was caused by not being able to run successfully the commands makemigrations and subsequently migrate. By error I had removed __init__.py from the migrations directory. After restoring the file and rerun the commands, that part was corrected. For the future, I can recommend the following script to clean/reset migrations (taken from one of the answers to this question).

#!/bin/sh
echo "Starting ..."

echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete

# Optional
echo ">> Deleting database"
find . -name "db.sqlite3" -delete

echo ">> Running manage.py makemigrations"
python manage.py makemigrations

echo ">> Running manage.py migrate"
python manage.py migrate

echo ">> Done"

The second part was to be able to adjust width of the fields. I have my fields defined in models.py, but by adding a widget call to the Meta class of the equivalent form class in forms.py I had power to adjust this, and more. Here is my forms.py:

class ChemRunForm(forms.ModelForm):
    class Meta:
        model = ChemRun
        widgets =       {'title': forms.TextInput(attrs={'style':'width: 500px;'})}
        exclude = ('owner')

NB: ChemRun is the models class, ChemRunForm is the forms class. title and owner are two of the fields for my model.

Hope this will help people in the future

👤Frank

Leave a comment