11👍
If you modified the fields in models. after that you run makemigrations that time it asking like this
^C(api_env)nyros@nyros:~/Desktop/santhi_projects/sample_api/sample_api$ python manage.py makemigrations
You are trying to add a non-nullable field 'provider' to content without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Select an option: 1
We select 1 option, then it will display like this
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now()
>>> timezone.now()
We given timezone.now() then makemigrations completed.
See your migrations last file, there is
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('providers', '0023_remove_content_provider'),
]
operations = [
migrations.AddField(
model_name='content',
name='provider',
field=models.ForeignKey(related_name='library', default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc), to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]
In above code observe this line
default=datetime.datetime(2016, 11, 1, 7, 15, 12, 655838, tzinfo=utc)
In that line forgeinkey field default value is datetime, Is it correct value?
No, so you have to give some string or object is a default value to that field.
Now you have to edit that value in that corresponding migration file, like this
default='some string'
then save and run the migrate command.
Try and let me know, Is it works or not.Thanks
5👍
Change your model field default value for auto_now_add=True
end_date = models.DateTimeField(auto_now_add=True)
start_date = models.DateTimeField(auto_now_add=True)
- Django – Difference between Database backed sessions and Cookie Based Session?
- Django template variable value to string literal comparison fails
4👍
Remove your migrations with these commands :
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
Then delete database file with rm db.sqlite
Then run your server
3👍
Goto migration folder “migrations
”
go to your migration file e.g"0054_comment_date.py"
change the 'default'
in line field=models.DateTimeField(auto_now_add=True, default=0)
from 0 or whatevere is there
to default='2012-09-04 06:00:00.000000-08:00
‘
2👍
Delete all the migrations manually. and then type
python manage.py makemigrations and python manage.py migrate
— Hope this may work .. worked for me.
- Why isn't admin.autodiscover() called automatically in Django when using the admin, why was it designed to be called explicitly?
- Django cascade delete on reverse foreign keys
- How do I handle file upload via PUT request in Django?
- Celery: auto discovery does not find tasks module in app
1👍
Just remove all files in migrations folder except “init.py”
and delete “db.sqlite3” i.e your database and now make changes that you want and migrate.Hope This helps 🙂
- How to send success message if we use django generic views
- Yuglify compressor can't find binary from package installed through npm
- Django override the form HTML label template?
- Error loading MySQLdb module: libmysqlclient.so.20: cannot open shared object file: No such file or directory
- ForeignKey to a model that is defined after/below the current model
1👍
I encountered with the same problem. The solution was to delete all default spaces in ‘migrations’ folder.
while you type ‘python manage.py migrate’ go up in terminal and find where the exact file in ‘migrations’ stopping the migrating. Then find all default arguments for DateField or DateTimeField and delete it. Those models don’t handle with default argument.
migrations.AlterField(
model_name='order',
name='start_date',
field=models.DateField(auto_now_add=True, ***BUG default=False BUG***),
preserve_default=False,
),
See the log in your terminal:
Running migrations:
Applying ang.0003_poll_end_date...Traceback (most recent call last):
in this file there is a bug. Go there and delete the default argument from DateField
- Always False Q object
- Determine if an attribute is a `DeferredAttribute` in django
- Very simple user input in django
1👍
You can just use end_date = models.DateTimeField(auto_now_add=True)
and it automatically creates a default date for you.
If this error not solved with this, then try deleting all your migrations. After this again use python manage.py makemigrations
and python manage.py makemigrations <app_name>
, then migrate, it should definitely work.
I followed this and this worked for me
- Pydev not recognizing python installation with django
- Creating an entire web application using django admin
- Can't open file 'django-admin.py': [Errno 2] No such file or directory
- PIL – libjpeg.so.8: cannot open shared object file: No such file or directory
0👍
Same issue here, when running through adding placeholders to my datefields, I accidentally entered “1” where “django.utils.timezone.now” should be, that is for the date-time default. Go into your last migration and find the “default=xxx” that is not like the rest and insert “django.utils.timezone.now”
- Django: prefetch related objects of a GenericForeignKey
- How can I automatically let syncdb add a column (no full migration needed)
- 'TemplateDoesNotExist' error with creating Sitemap for Django app
0👍
In case all the above solutions do not work for you, like in my case. You can easy solve it with these simple steps.
- Go into your apps directory and delete all previous migrations leaving only the
pycache
init.py
-
Go into your main project directory and delete the database. (NB: no side effect, please just do it)
-
With all done, open your terminal and run
$ python manage.py makemigrations
$ python manage.py migrate
Done. Thanks.
- Django Model Method or Calculation as Field in Database
- How to add report section to the Django admin?
- Self.model() in django custom UserManager
0👍
Just running:
python manage.py makemigrations
and
python manage.py migrate
worked for me, I refer Suceel answer 👍
- Annotate django query if filtered row exists in second table
- Provide tab title with reportlab generated pdf
- Django multi-table inheritance alternatives for basic data model pattern
- How do I setup messaging and session middleware in a Django RequestFactory during unit testing
- Django Forms: Hidden model field?
0👍
If you follow the instructions below you’ll be good to have your error fixed.
datetime = models.DateTimeField(auto_now_add=True)
- you will be provided with two options in which you have to choose option >> 1 Provide the default…
- and then type
timezone.now
- and finally press enter
- How to make follower-following system with django model
- Is it correct to modify old migration files in Django?
-2👍
I had the same problem. Look at all the models.DateTimeField lines in the .py file generated after calling makemigrations. There you will notice that some of the DateTimeField have a wrong default value. Replace those with models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now) for example.
- Using django models across apps?
- Git push heroku master: Heroku push rejected, no Cedar-supported app detected