Django - makemigrations - No changes detected

前端 未结 30 1305
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 04:44

I was trying to create migrations within an existing app using the makemigrations command but it outputs \"No changes detected\".

Usually I create new apps using the

相关标签:
30条回答
  • 2020-12-02 05:41

    My problem (and so solution) was yet different from those described above.

    I wasn't using models.py file, but created a models directory and created the my_model.py file there, where I put my model. Django couldn't find my model so it wrote that there are no migrations to apply.

    My solution was: in the my_app/models/__init__.py file I added this line: from .my_model import MyModel

    0 讨论(0)
  • 2020-12-02 05:43

    A very dumb issue you can have as well is to define two class Meta in your model. In that case, any change to the first one won't be applied when running makemigrations.

    class Product(models.Model):
        somefield = models.CharField(max_length=255)
        someotherfield = models.CharField(max_length=255)
    
        class Meta:
            indexes = [models.Index(fields=["somefield"], name="somefield_idx")]
    
        def somefunc(self):
            pass
    
        # Many lines...
    
        class Meta:
            indexes = [models.Index(fields=["someotherfield"], name="someotherfield_idx")]
    
    0 讨论(0)
  • 2020-12-02 05:46

    Try registering your model in admin.py, here's an example:- admin.site.register(YourModelHere)

    You can do the following things:- 1. admin.site.register(YourModelHere) # In admin.py 2. Reload the page and try again 3. Hit CTRL-S and save 4. There might be an error, specially check models.py and admin.py 5. Or, at the end of it all just restart the server

    0 讨论(0)
  • 2020-12-02 05:47

    I know this is an old question but I fought with this same issue all day and my solution was a simple one.

    I had my directory structure something along the lines of...

    apps/
       app/
          __init__.py
          app_sub1/
               __init__.py
               models.py
          app_sub2/
               __init__.py
               models.py
          app_sub3/
               __init__.py
               models.py
       app2/
          __init__.py
          app2_sub1/
               __init__.py
               models.py
          app2_sub2/
               __init__.py
               models.py
          app2_sub3/
               __init__.py
               models.py
        main_app/
          __init__.py
          models.py
    

    And since all the other models up until the one I had a problem with were being imported somewhere else that ended up importing from main_app which was registered in the INSTALLED_APPS, I just got lucky that they all worked.

    But since I only added each app to INSTALLED_APPS and not the app_sub* when I finally added a new models file that wasn't imported ANYWHERE else, Django totally ignored it.

    My fix was adding a models.py file to the base directory of each app like this...

    apps/
       app/
          __init__.py
          models.py <<<<<<<<<<--------------------------
          app_sub1/
               __init__.py
               models.py
          app_sub2/
               __init__.py
               models.py
          app_sub3/
               __init__.py
               models.py
       app2/
          __init__.py
          models.py <<<<<<<<<<--------------------------
          app2_sub1/
               __init__.py
               models.py
          app2_sub2/
               __init__.py
               models.py
          app2_sub3/
               __init__.py
               models.py
        main_app/
          __init__.py
          models.py
    

    and then add from apps.app.app_sub1 import * and so on to each of the app level models.py files.

    Bleh... this took me SO long to figure out and I couldn't find the solution anywhere... I even went to page 2 of the google results.

    Hope this helps someone!

    0 讨论(0)
  • 2020-12-02 05:48
    1. Make sure your app is mentioned in installed_apps in settings.py
    2. Make sure you model class extends models.Model
    0 讨论(0)
  • 2020-12-02 05:49

    You should add polls.apps.PollsConfig to INSTALLED_APPS in setting.py

    0 讨论(0)
提交回复
热议问题