Django migration error with GenericIPAddressField - django

hey I have working on some project now I want to add IP address fields in every model
ip_address = models.GenericIPAddressField()
adding like this
now when I migrate it I got an error
like this
if value and ':' in value:
TypeError: argument of type 'int' is not iterable
0005_remove_documentvault_ip_address
class Migration(migrations.Migration):
dependencies = [
('master', '0005_remove_documentvault_ip_address'),
]
operations = [
migrations.AddField(
model_name='documentvault',
name='ip_address',
field=models.GenericIPAddressField(default=1),
preserve_default=False,
),
]
Erorr Message:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, master, sessions, users
Running migrations:
Applying master.0006_documentvault_ip_address...Traceback (most recent call last):
File "./manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/mysql/schema.py", line 42, in add_field
super().add_field(model, field)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 421, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 168, in column_sql
default_value = self.effective_default(field)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 239, in effective_default
return field.get_db_prep_save(default, self.connection)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 790, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1936, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/panacea/Documents/src/easy tailor/EasyTailor/venv/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 1943, in get_prep_value
if value and ':' in value:
TypeError: argument of type 'int' is not iterable

The migration (and perhaps your model too) has default=1. That's not a valid IP address.
You can either replace the default in your model with a valid IP address, or remove the default and set null=True.
Then delete the 0006 migration (back it up first to be safe, or make sure it's in version control). Finally, rerun makemigrations and migrate.

Related

Django renaming class casued AttributeError: 'str' object has no attribute '_meta'

How can I solve this issue, I renamed a through Model class and now getting the following error when trying to migrate:
(django_env) UKC02TQH6UHV22:pcc_django rki23$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, microblog, project_portal, sessions, url_tree
Running migrations:
Applying project_portal.0012_auto_20200205_1241...Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/Users/rki23/opt/anaconda3/envs/django_env/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 524, in alter_field
not new_field.remote_field.through._meta.auto_created):
AttributeError: 'str' object has no attribute '_meta'
(django_env) UKC02TQH6UHV22:pcc_django rki23$
What do I need to do to fix this please?
THis is the migration file that is causing the issue. The name change was for a many to many field.
# Generated by Django 2.2.5 on 2020-02-05 12:41
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('project_portal', '0011_auto_20200127_0953'),
]
operations = [
migrations.RenameModel(
old_name='CategorySatisfied',
new_name='TaskComplete',
),
migrations.AlterField(
model_name='updatecategory',
name='projects',
field=models.ManyToManyField(blank=True, related_name='task_complete', through='project_portal.CategorySatisfied', to='project_portal.Project'),
),
]

TypeError: expected string or bytes-like object although datetime field not used Django

I'm trying to migrate my datas, but django returns me TypeError: expected string or bytes-like object error, although I just tried using datetime once, then deleted it, but still it returns an error. Here are my codes:
models.py
class Applicant(models.Model):
name = models.CharField(max_length=20)
surname = models.CharField(max_length=30)
phone = models.CharField(max_length=15)
email = models.EmailField(max_length=40)
motivation_letter = models.TextField(max_length=200)
is_accepted = models.BooleanField(default=False)
photo = models.FileField(upload_to='static/applicant_photos')
def __str__(self):
return self.name
an error:
Operations to perform:
Apply all migrations: admin, auth, ccapp, contenttypes, sessions
Running migrations:
Applying ccapp.0003_applicant_birth_date...Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 327, in add_field
self._remake_table(model, create_field=field)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 188, in _remake_table
self.effective_default(create_field)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1431, in get_db_prep_value
value = self.get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1410, in get_prep_value
value = super().get_prep_value(value)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1270, in get_prep_value
return self.to_python(value)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1371, in to_python
parsed = parse_datetime(value)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/utils/dateparse.py", line 106, in parse_datetime
match = datetime_re.match(value)
TypeError: expected string or bytes-like object

How to erase previously set one-time default value

In my django app, I had to set a one-time default value for one of the fields when I tried to migrate.
When I did this, however, I accidentally set a wrong value. Ever since then, the same error pops up when I try to migrate:
ValueError: invalid literal for int() with base 10: '123e4567-e89b-12d3-a456-426655440000'
This is despite the fact that I rectified the issue in my code - it seems like this issue is stuck forever. Anything I can do to fix this?
Traceback:
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 112, in database_forwards
field,
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 433, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 161, in column_sql
default_value = self.effective_default(field)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
return field.get_db_prep_save(self._effective_default(field), self.connection)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 959, in get_db_prep_value
value = self.get_prep_value(value)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 968, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '123e4567-e89b-12d3-a456-426655440000'
The wrong default is probably still present in your migration file.
If you have not applied this migration yet, you can just delete it and recreate it with python manage.py migrate.

Django migrate command fails with change in foreign key related name

I have a model with a foreign key field. I want to give the foreign key a related name. However, migration fails because of the following error:
IndexError: list index out of range
I'm using Django==1.10.5
Database backend is PostgreSQL
models.py Before/After migrations
Before:
class Contest_participants(models.Model):
contest = models.ForeignKey(Contest)
After:
class Contest_participants(models.Model):
contest = models.ForeignKey(Contest, related_name='contests')
The makemigrations command works fine, but the migrate command fails with the above noted error.
Complete Traceback Error:
Running migrations:
Applying teams.0013_auto_20171031_1225...Traceback (most recent call last):
File "manage.py", line 19, in <module>
execute_from_command_line(sys.argv)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
output = self.handle(*args, **options)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 204, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 495, in alter_field
old_db_params, new_db_params, strict)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/postgresql/schema.py", line 117, in _alter_field
new_db_params, strict,
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 504, in _alter_field
fk_names = self._constraint_names(model, [old_field.column], foreign_key=True)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 931, in _constraint_names
constraints = self.connection.introspection.get_constraints(cursor, model._meta.db_table)
File "/home/aps/Documents/neuron/venv/lib/python3.5/site-packages/django/db/backends/postgresql/introspection.py", line 178, in get_constraints
"foreign_key": tuple(used_cols[0].split(".", 1)) if kind.lower() == "foreign key" else None,
IndexError: list index out of range
EDIT: Migrations
class Migration(migrations.Migration):
dependencies = [
('teams', '0012_auto_20170904_1524'),
]
operations = [
migrations.AlterField(
model_name='contest_participants',
name='contest',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='contests', to='contest.Contest'),
),
]

Django User as ForeignKey Error

I just simply want to have multiple ssh-session to be associated with each user
from django.contrib.auth.models import User
class Sshsession(models.Model):
session_name=models.CharField(max_length=20)
# Other session attributes
usr=models.ForeignKey(User)
When I do migrate I get an error
int() argument must be a string or a number, not 'User'
I tried doing User.id but didnt help.
Full Trace:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/djang/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 382, in add_field
definition, params = self.column_sql(model, field, include_default=True)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 145, in column_sql
default_value = self.effective_default(field)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 210, in effective_default
default = field.get_db_prep_save(default, self.connection)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 912, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save
prepared=False)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 968, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/ubuntu/blahblah/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 976, in get_prep_value
return int(value)
TypeError: int() argument must be a string or a number, not 'User'