这是一个Post模型,当我在django admin中打开它来添加帖子时,我看到了一个字段内容类型,但没有下拉列表来选择内容类型。我在这里做错了吗?
class Post(models.Model):
FEATURE_LIMITS = models.Q(app_label=u'forums', model=u'forum')
content_type = models.ForeignKey(ContentType, limit_choices_to=FEATURE_LIMITS, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
admin.py 帖子
# imports
admin.site.register(posts_models.Post)
有一个模型论坛,与帖子有一般关系。
class Forum(models.Model):
# other fields
posts = GenericRelation(Post)
在django admin上看不到帖子字段(其他字段可见)。
论坛 的admin.py
# imports
admin.site.register(forums_models.Forum)
如果我对admin.py进行更改:
class PostInline(GenericTabularInline):
model = Post
class ForumAdmin(admin.ModelAdmin):
inlines = [
PostInline,
]
admin.site.register(forums_models.Forum, ForumAdmin)
然后在点击http://localhost:8000/admin/forums/forum/add/
时,页面会永远加载。
答案 0 :(得分:1)
您似乎错误地配置了应用程序。
我的项目结构
let attrStr = try! NSAttributedString(
data: "Your baby in week three\n" +
"Three weeks into your pregnancy, and you might be wondering just how this little miracle occurred (as in the science bit we will assume you know the other part!). \n" +
"\n" +
"So here goes: the joining of your partner's sperm and your egg at the moment of conception resulted in a ball of cells (or a blastocyst, if you want to get really technical) which is now starting the 6 day journey from the fallopian tube to the uterus where it will attach itself to the lining of your womb and in nine months time, will result in a real live baby in your arms! \n" +
"\n" +
"Amazingly, although you will not know for a few months (or until the birth) the <b><font color=#00690c >sex of your baby</font></b> has already been determined. The fertilised egg contains <b><font color=#e3007b>46</font></b> chromosomes with <b><font color=#e3007b>23</font></b> from you and <b><font color=#e3007b>23</font></b> from the dad. You as mum will always provide the <b><font color=#e3007b>X</font></b> chromosome but the dad can provide an <b><font color=#e3007b>X</font></b> or <b><font color=#e3007b>Y</font></b> chromosome. <b><font color=#e3007b>XX</font></b> and you will be having a girl, <b><font color=#e3007b>XY</font></b> and you will have a little boy in 9 months time!".dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
trimestercontent.attributedText = attrStr
所以 posts / models.py
app
|-settings/
|-forums/
|--models.py
|--admin.py
|-posts/
|--models.py
|--admin.py
<强>文章/ admin.py 强>
class Post(models.Model):
FEATURE_LIMITS = models.Q(app_label='forums', model='forum')
name = models.CharField(max_length=100)
content_type = models.ForeignKey(ContentType, limit_choices_to=FEATURE_LIMITS, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
<强>论坛/ models.py 强>
from .models import Post
class PostInline(GenericTabularInline):
model = Post
<强>论坛/ admin.py 强>
class Forum(models.Model):
# other fields
name = models.CharField(max_length=100)
posts = GenericRelation('posts.Post')
一切都像魅力一样。
我猜你有无限循环因为试图在错误的app中注册模型。(app from .models import Forum
from posts.admin import PostInline
@admin.register(Forum)
class ForumAdmin(admin.ModelAdmin):
inlines = [
PostInline,
]
中的模型Forum
)
答案 1 :(得分:0)
我正在使用admin.widgets.ForeignKeyRawIdWidget
widget来阻止外键字段正常显示。从那以后
content_type = models.ForeignKey(ContentType, limit_choices_to=FEATURE_LIMITS, on_delete=models.CASCADE)
content_type是外键字段,没有下拉列表。 要防止这种情况发生,请在admin.py中进行以下更改:
class PostAdmin(admin.ModelAdmin):
raw_id_field_excludes = 'content_type'
admin.site.register(Post, PostAdmin)