Deployment - Heroku

Hi All (Ken more likely),

I followed Heroku’s deployment guide, and I’m getting the following error:

-----> $ python manage.py collectstatic --noinput
       Traceback (most recent call last):
         File "/tmp/build_e076db66/manage.py", line 22, in <module>
           main()
         File "/tmp/build_e076db66/manage.py", line 18, in main
           execute_from_command_line(sys.argv)
         File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
           utility.execute()
         File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
           django.setup()
         File "/app/.heroku/python/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
           apps.populate(settings.INSTALLED_APPS)
         File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 122, in populate
           app_config.ready()
         File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/admin/apps.py", line 27, in ready
           self.module.autodiscover()
         File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
           autodiscover_modules('admin', register_to=site)
         File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
           import_module('%s.%s' % (app_config.name, module_to_search))
         File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
           return _bootstrap._gcd_import(name[level:], package, level)
         File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
         File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
         File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
         File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
         File "<frozen importlib._bootstrap_external>", line 850, in exec_module
         File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
         File "/tmp/build_e076db66/account/admin.py", line 2, in <module>
           from .models import Profile,Leads,Company,Client,CallReport,Deal,LeadEntry
       ModuleNotFoundError: No module named 'account.models'
 !     Error while running '$ python manage.py collectstatic --noinput'.
       See traceback above for details.
       You may need to update application code to resolve this error.
       Or, you can disable collectstatic for this application:
          $ heroku config:set DISABLE_COLLECTSTATIC=1
       https://devcenter.heroku.com/articles/django-assets

I’m not sure why is failing on that line in admin.py

from django.contrib import admin
from .models import Profile,Leads,Company,Client,CallReport,Deal,LeadEntry

# Register your models here.
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
    list_display = ['user','id','photo','role','location']

@admin.register(Leads)
class LeadAdmin(admin.ModelAdmin):
    list_display = ['estimated_closing_date',
                    'expected_revenue', 'country', 'status',
                    'services','agent','point_of_contact','application','sub_category','created_at',
                    'installation_date','license_term']

@admin.register(Company)
class CompanyAdmin(admin.ModelAdmin):
    list_display = ['company','category']

@admin.register(CallReport)
class CallReportAdmin(admin.ModelAdmin):
    list_display = ['minutes_of_meeting','client_POC','location',
                    'title','date','ACTeQ_representative',
                    'company','tags']

@admin.register(Client)
class ClientReport(admin.ModelAdmin):
    list_display = ['firstname','lastname','company','position','country','email','phone',
                  'background','technical_rank','financial_rank','tesserACT_status']

@admin.register(Deal)
class DealReport(admin.ModelAdmin):
    list_display = ['id','project_id', 'sales_order','agent', 'service',
                    'closing_date','client','licenses',
                    'revenue','comments','company','sales_order','installation_date',
                    'license_term']

@admin.register(LeadEntry)
class LeadEntryForm(admin.ModelAdmin):
    list_display = ['id','lead_id','revenue','date','probability','stage']



What does your project’s file structure look like? And are you using any buildpacks that would change the file structure? It seems like the parent directory may be missing an __init__.py file, but I don’t see how that would work locally though. Hence the question if you’re doing anything funky with Heroku.

Tim,

Thanks, there was an error with the repository, so we finally managed to get the app working, however, as part of the CD/CI, I’m continuing working on the app, and I have made some changes just to see if I can update my project, and well is not working.

I made some changes to the database on my local project → push to github and then on to Heroku, and since I made some changes to the DB, I ran the correspondent migrations:

C:\Users\fcolina>heroku run python manage.py makemigrations
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
Running python manage.py makemigrations on ⬢ acteq-crm... up, run.6883 (Free)
Migrations for 'account':
  account/migrations/0002_auto_20220107_0335.py
    - Add field installation_date to deal
    - Add field license_term to deal
    - Add field installation_date to leads
    - Add field license_term to leads
    - Alter field location on callreport
    - Alter field country on client
    - Alter field email on client
    - Alter field firstname on client
    - Alter field lastname on client
    - Alter field tesserACT_status on client
    - Alter field id on deal

C:\Users\fcolina>heroku run python manage.py migrate
 »   Warning: heroku update available from 7.53.0 to 7.59.2.
Running python manage.py migrate on ⬢ acteq-crm... up, run.3358 (Free)
Operations to perform:
  Apply all migrations: account, admin, auth, contenttypes, sessions, taggit
Running migrations:
  No migrations to apply.
  Your models in app(s): 'account' have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

I have done it several times and is still not working.

Any ideas on how to fix this error?

When you run heroku run ... makemigrations it spins up a new dyno, it doesn’t re-use an existing one (that’s a different heroku command). So what’s happening is that you’re spinning up a dyno, it creates a migration file in the local file system, then the whole dyno disappears. When you run heroku run ... migrate it’s working against the migrations that were committed to your git repo and doesn’t see the migration file created in the other dyno’s command.

You should run makemigrations locally, commit that to your repo, and push it to heroku where you can then use heroku run ... migrate to modify your database.

Tim,

Thanks, everything is working now, you are completely correct, and I just found this guide:

https://help.heroku.com/GDQ74SU2/django-migrations

As a plug for a repo I help maintain, there’s django-safemigrate which has been built specifically for Heroku and adding the safemigrate command to Heroku’s release phase tasks.