内联Django管理员:没有ForeignKey [英] Inline in Django admin: has no ForeignKey

查看:293
本文介绍了内联Django管理员:没有ForeignKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Django模型:

 类作者(models.Model):
django_login = models.OneToOneField(User,null = True,blank = True)
user_nicename = models.CharField(max_length = 60,blank = True)
user_url = models.CharField(max_length = blank = True)
user_email = models.CharField(max_length = 100,blank = True)
display_name = models.CharField(max_length = 250)
note = models.TextField(verbose_name ='O autorze')
def __unicode __(self):
return self.display_name

class照片(models.Model):
item = models.ForeignKey(作者)
image = ThumbnailImageField(upload_to ='photos')
caption = models.CharField(max_length = 250,blank = True,verbose_name = uPodpis)
def __unicode __(self):
return self.caption

要获取内联照片,我在admin.py:

 inlines = [PhotoInline] 

我收到错误: / admin / metainf / author / 11 /

中的异常

 < class'metainf.models.Author'>没有ForeignKey到< class'metainf.models.Author'> 

为什么?

解决方案

内联模型应该有一个ForeignKey到父模型。要获得照片作为作者中的内联您的模型代码是正常的。但是您的管理员代码应该如下:

  class PhotoInline(admin.StackedInline):
model = Photo

class AuthorAdmin(admin.ModelAdmin):
list_display =('display_name','user_email')
inlines = [PhotoInline]
/ pre>

阅读更多信息 here


I have two Django models:

class Author(models.Model):
    django_login = models.OneToOneField(User, null=True, blank=True)
    user_nicename = models.CharField(max_length=60, blank=True)
    user_url = models.CharField(max_length=100, blank=True)
    user_email =  models.CharField(max_length=100, blank=True)
    display_name =  models.CharField(max_length=250)
    note = models.TextField(verbose_name='O autorze')
    def __unicode__(self):
        return self.display_name

class Photo(models.Model):
    item = models.ForeignKey(Author)
    image = ThumbnailImageField(upload_to='photos')
    caption = models.CharField(max_length=250, blank=True, verbose_name = u"Podpis")
    def __unicode__(self):
        return self.caption

To get inline photos, I have in admin.py:

class PhotoInline(admin.StackedInline):
    model = Author

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('display_name','user_email')
    inlines = [PhotoInline]

I get an error: Exception at /admin/metainf/author/11/

<class 'metainf.models.Author'> has no ForeignKey to <class 'metainf.models.Author'>

Why?

解决方案

The inline model should be having a ForeignKey to the parent model. To get Photo as inline in Author your models code is fine. But your admin code should be as follows:

class PhotoInline(admin.StackedInline):
    model = Photo

class AuthorAdmin(admin.ModelAdmin):
    list_display = ('display_name','user_email')
    inlines = [PhotoInline]

Read more info here.

这篇关于内联Django管理员:没有ForeignKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆