6
votes

I've searched every Stack Overflow question on this error but none of the responses helped. I'm getting this error when trying to access the admin page of this particular model (AgentBasicInfo).

'manage.py makemigrations' works fine. 'manage.py migrate' also works fine. 'manage.py runserver' works fine, the whole website works fine until I try to go onto the admin page of this model.

The app is correctly installed in INSTALLED_APPS in settings.py. I am using Postgres for the database.

I have tried...

  1. Deleting migrations and re-running makemigrations/migrate
  2. Deleting the entire migrations folder for this app and rerunning makemigrations/migrate
  3. Deleting all the migrations from all my apps and re-running makemigrations/migrate
  4. I have tried running 'manage.py migrate' and 'mangae.py migrate app_name'. I still get the same error.

This model (see code below) is quite basic. I have several other models in my project and they work just fine in the admin, but just this particular model doesn't work.

models.py

class AgentBasicInfo(models.Model):

    preferred_email = models.EmailField()
    office_phone_number = models.IntegerField()
    brokerage_of_agent = models.CharField(max_length=50)
    agent_title = models.CharField(max_length=20)

    def __str__(self):
        return self.preferred_email

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'lagger123',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Picture of the error for reference

0001_initial.py

from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='AgentBasicInfo',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('preferred_email', models.EmailField(max_length=254)),
                ('office_phone_number', models.IntegerField()),
                ('brokerage_of_agent', models.CharField(max_length=50)),
                ('agent_title', models.CharField(max_length=20)),
            ],
        ),
    ]

Output of manage.py showmigrations:

accounts
 [X] 0001_initial
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
coresite
 (no migrations)
databases
 (no migrations)
manage_listings
 [X] 0001_initial
search_listings
 (no migrations)
sessions
 [X] 0001_initial
teams
 (no migrations)
2
How do the migrations look that are created for this app?schwobaseggl
@schwobaseggl Here is what 'manage.py migrate' currently says: Operations to perform: Apply all migrations: accounts, admin, auth, contenttypes, manage_listings, sessions Running migrations: No migrations to apply.Valachio
Can you post the content of accounts/migrations/0001_initial.py?schwobaseggl
I should also mention AgentBasicInfo is attached to a ModelForm, and the ModelForm is being used in a function-based view which is handling 2 different forms.Valachio
@schwobaseggl Just postedValachio

2 Answers

1
votes

Open db command line.

python manage.py dbshell

And try this

delete from django_migrations where app='app_name';

Then delete migration files and run migration commands.

0
votes

I also had this problem and tried:

python manage.py dbshell

But then I got this error:

CommandError: You appear not to have the 'psql' program installed or on your path.

This was due windows not finding psql in my environment path. As an alternative, you can get it done by reverting changes (that is if you had previous changes in you git repository.

For me I used this method:

git checkout <commit hash> (which did not have the error)

After that pull the changes:

git pull <remote> <branch>

Then finally:

git push origin main

Hope this helps for the ones with git repositories. I welcome any corrections.