如何用迁移django解决错误?

时间:2017-04-11 16:10:12

标签: django

admin.py

from django.contrib import admin
from main.models import *
from django.db.models import TextField
from file_picker.wymeditor.widgets import WYMeditorWidget

@admin.register(MainUser)
class MainUserAdmin(admin.ModelAdmin):
    list_display = ('username', 'first_name', 'second_name')

class BlogAdmin(admin.ModelAdmin):
    prepopulated_fields = {'slug':('title', )}
    list_display = ('title', 'time')
    formfield_overrides = {TextField: { 'widget': WYMeditorWidget({}) } }
    class Media:
        js = ('http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js', )

admin.site.register(Blog, BlogAdmin)

class PostLecture(admin.ModelAdmin):
    list_display = ('title')    

admin.site.register(PostLecture)

models.py

class PostLecture(models.Model):
    title = models.CharField(max_length=200)
    pdf = models.FileField()
    time = models.DateTimeField(auto_now_add = True)


    def __unicode__(self):
        return self.title

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"id": self.id})

    class Meta:
        ordering = ['-time']

我使用命令进行迁移(makemigration和migrate)。在管理面板中运行服务器后,没有我的迁移。我有这样的错误:

  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
    django.setup()
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/nursultan/projects/diplomproject/diplomproject/main/admin.py", line 22, in <module>
    admin.site.register(PostLecture)
  File "/Users/nursultan/projects/diplomproject/env/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 86, in register
    for model in model_or_iterable:
TypeError: 'MediaDefiningClass' object is not iterable

如何解决?错误在哪里?

1 个答案:

答案 0 :(得分:1)

您没有注册PostLecture模型(与Blog一样)。

请改为:

class PostLectureAdmin(admin.ModelAdmin):
    list_display = ('title', )

admin.site.register(PostLecture, PostLectureAdmin)

另外,作为旁注,在PostLecture模型中,删除__unicode____str__。如果你正在使用python 2,请使用__unicode__;如果你正在使用python 3,请使用__str__