开发者

The model is AlreadyRegistered at/

开发者 https://www.devze.com 2023-02-04 19:14 出处:网络
I\'m getting an error: AlreadyRegistered at / The model Post is already registered Why is this happening, and how do I fix it? Here\'s my models.py :

I'm getting an error:

AlreadyRegistered at / The model Post is already registered

Why is this happening, and how do I fix it? Here's my models.py :

from django.db import models from
django.contrib import admin


class Post(models开发者_C百科.Model):
    title = models.CharField(max_length=60)
    body = models.TextField()
    created = models.DateTimeField(auto_now_add = True)

    def __unicode__(self):
        return self.title


class PostAdmin(admin.ModelAdmin):
    search_fields = ["title"]

admin.site.register(Post,PostAdmin)


This generally happens when you register you models in models.py file which might be imported into some other modules. The recommended way is to create a separate admin.py file for registering your models.


models.py

from django.db import models

class Post(models.Model): 
    title = models.CharField(max_length=60) 
    body = models.TextField()   
    created = models.DateTimeField(auto_now_add = True)

    def __unicode__(self):
        return self.title

admin.py

from django.contrib import admin

class PostAdmin(admin.ModelAdmin): 
    search_fields = ["title"]

admin.site.register(Post,PostAdmin)
0

精彩评论

暂无评论...
验证码 换一张
取 消