Django admin not showing models

后端 未结 6 1665
悲&欢浪女
悲&欢浪女 2021-02-07 09:27

I\'m damned if I can figure out why my models are not showing in the admin.

models.py looks like this:

from django.db import models

class Publication(mo         


        
6条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 10:01

    Did you run syncdb after changing your models.py file?

    Go to the base directory of your project (where manage.py is) and run

    python manage.py syncdb
    

    Edit: Since you already tried this, add a str or unicode method to your class like this:

    class Resource(models.Model):
    
        title = models.CharField(max_length=128, blank=True)
    
        def __str__(self): # __str__ for Python 3, __unicode__ for Python 2
            return self.name
    

    Edit 2: Funnily enough, I ran into the exact same issue today working on my own projects. If the str code above still doesn't work for you, double check that you've actually registered your models in admin.py:

    from MyApp.models import MyModel
    admin.site.register(MyModel)
    

    Edit 3: Finally, make sure your admin.py file with the above code is actually inside the directory of the app in question (i.e. the same directory with your models.py that defines MyModel).

提交回复
热议问题