Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Django model class names #837

Closed
jonfroehlich opened this issue Dec 4, 2019 · 5 comments
Closed

Refactor Django model class names #837

jonfroehlich opened this issue Dec 4, 2019 · 5 comments

Comments

@jonfroehlich
Copy link
Collaborator

When we originally wrote some models like Project_header and Project_umbrella, we didn't follow best naming practices. And it's bothered me ever since. These should be ProjectHeader and ProjectUmbrella respectively.

However, it's not a matter of simply renaming these in code because they are actually table names in PostgreSQL. So, we have to write custom migration code to handle this. Shouldn't be too difficult but will require some work.

@jonfroehlich
Copy link
Collaborator Author

Or I wonder if we could just manually rename the columns in PostgresSQL after changing the model names...

@jonfroehlich
Copy link
Collaborator Author

jonfroehlich commented Dec 19, 2021

Looks like Django 2 helps you refactor/rename model names

https://stackoverflow.com/a/26241640

In django 2.0 if you change your model name, the ./manage.py makemigrations myapp command will ask you if you renamed your model. E.g.: Did you rename the myapp.Foo model to Bar? [y/N] If you answer 'y' your migration will contain the migration.RenameModel('Foo', 'Bar') Same counts for the renamed fields :-)

@jonfroehlich
Copy link
Collaborator Author

@jonfroehlich
Copy link
Collaborator Author

OK, I just did this on my local system and it was painless. I think I'll have to get Jason involved, however, as it requires running migration commands.

(base) jonfroehlich@Jons-MacBook-Pro-2 makeabilitylabwebsite % docker exec -it makeabilitylabwebsite_website_1 bash
apache@31c0a0c7ed35:/code$ python manage.py makemigrations
Was the model website.Project_header renamed to ProjectHeader? [y/N] y
Was the model website.Project_Role renamed to ProjectRole? [y/N] y
Was the model website.Project_umbrella renamed to ProjectUmbrella? [y/N] y
Migrations for 'website':
  website/migrations/0002_rename_project_header_projectheader_and_more.py
    - Rename model Project_header to ProjectHeader
    - Rename model Project_Role to ProjectRole
    - Rename model Project_umbrella to ProjectUmbrella

@jonfroehlich
Copy link
Collaborator Author

This has now been addressed and changes pushed to both -test and production

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant