django,管理模板错误渲染时捕获异常:“NoneType”对象没有属性“标签”;

发布于 2024-08-22 20:10:40 字数 2114 浏览 4 评论 0原文

美好的一天伙计们! 在项目中,除其他外,都有模型:

class Category(models.Model):

    name = models.CharField(max_length = 50, blank = False, null = False)

    def __unicode__(self):
        return "Category %s" % self.name

    class Meta:
        db_table = "categories"
        managed = False


class Site(models.Model):

    user = models.ForeignKey(User, blank = False, null = False, db_column = "user_id")
    name = models.URLField(verify_exists = True, blank = False, null = False)
    categories = models.ManyToManyField(Category, blank = True, through = "CategorySites", verbose_name = "Category")

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = "sites"
        managed = False



class CategorySites(models.Model):

    site = models.ForeignKey(Site, blank = False, null = False, db_column = "site_id")
    category = models.ForeignKey(Category, blank = False, null = False, db_column = "category_id")

    def __unicode__(self):
        return "Relation between site %s and category %s" % (self.site.name, self.category.name)

    class Meta:
        db_table = "categories_sites"
        managed = False

因此,正如您所见,存在多对多关系。一般来说它工作得很好 - 我可以通过manage.py shell或服务器端功能添加和管理模型。 我想在管理站点上启用编辑这种类型的关系,因此,我为站点添加了管理模型:

 class SiteAdmin(admin.ModelAdmin):

    list_display = ('id', 'name')
    list_filter = ('name', 'categories')

    fieldsets = (
        (None, {"fields": ("categories",)}),
    )

    def queryset(self, request):
        qs = super(SiteAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs
        else:
            return qs.filter(user = request.user)

    def has_change_permission(self, request, obj=None):
        if not obj:
            return True # So they can see the change list page
        if request.user.is_superuser or obj.user == request.user:
            return True
        else:
            return False

    has_delete_permission = has_change_permission

但是当我进入管理->站点->添加站点(或编辑)时,django 抛出捕获异常渲染时: 'NoneType' 对象没有属性 'label' 。 如果可以修复它怎么办?

good day guys!
in project, among others, have models:

class Category(models.Model):

    name = models.CharField(max_length = 50, blank = False, null = False)

    def __unicode__(self):
        return "Category %s" % self.name

    class Meta:
        db_table = "categories"
        managed = False


class Site(models.Model):

    user = models.ForeignKey(User, blank = False, null = False, db_column = "user_id")
    name = models.URLField(verify_exists = True, blank = False, null = False)
    categories = models.ManyToManyField(Category, blank = True, through = "CategorySites", verbose_name = "Category")

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = "sites"
        managed = False



class CategorySites(models.Model):

    site = models.ForeignKey(Site, blank = False, null = False, db_column = "site_id")
    category = models.ForeignKey(Category, blank = False, null = False, db_column = "category_id")

    def __unicode__(self):
        return "Relation between site %s and category %s" % (self.site.name, self.category.name)

    class Meta:
        db_table = "categories_sites"
        managed = False

so, as you see there is many-to-many relation. generally it work well - i can add and manage models through manage.py shell, or server-side functions.
I want to enable editing this type of relation on admin site, so, i've added admin model for Sites:

 class SiteAdmin(admin.ModelAdmin):

    list_display = ('id', 'name')
    list_filter = ('name', 'categories')

    fieldsets = (
        (None, {"fields": ("categories",)}),
    )

    def queryset(self, request):
        qs = super(SiteAdmin, self).queryset(request)
        if request.user.is_superuser:
            return qs
        else:
            return qs.filter(user = request.user)

    def has_change_permission(self, request, obj=None):
        if not obj:
            return True # So they can see the change list page
        if request.user.is_superuser or obj.user == request.user:
            return True
        else:
            return False

    has_delete_permission = has_change_permission

but when i'm going in admin->sites->Add site (or edit) django throws Caught an exception while rendering: 'NoneType' object has no attribute 'label' .
how can if fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仙女 2024-08-29 20:10:40

为此,您需要将未记录的 formfield_for_manytomany 方法添加到您的 SiteAdmin 类中:

from django.contrib.admin import widgets

class SitebAdmin(admin.ModelAdmin):

   list_display = ('id', 'name')
   list_filter = ('name', 'categories')

   def formfield_for_manytomany(self, db_field, request, **kwargs):
      if db_field.name == 'categories':
         kwargs['widget'] = widgets.FilteredSelectMultiple(
              db_field.verbose_name, (db_field.name in self.filter_vertical))
      return super(SitebAdmin, self).formfield_for_foreignkey(
         db_field, request, **kwargs)

   fieldsets = (
       (None, {
            "fields": ("name", "categories",)
       }),
   )

以覆盖默认情况下不显示通过选项指定的模型的多个选择小部件 文档

虽然这有效,但我仍然认为这不应该在 Django 中导致错误。

For that you need to add undocumented formfield_for_manytomany method to your SiteAdmin class:

from django.contrib.admin import widgets

class SitebAdmin(admin.ModelAdmin):

   list_display = ('id', 'name')
   list_filter = ('name', 'categories')

   def formfield_for_manytomany(self, db_field, request, **kwargs):
      if db_field.name == 'categories':
         kwargs['widget'] = widgets.FilteredSelectMultiple(
              db_field.verbose_name, (db_field.name in self.filter_vertical))
      return super(SitebAdmin, self).formfield_for_foreignkey(
         db_field, request, **kwargs)

   fieldsets = (
       (None, {
            "fields": ("name", "categories",)
       }),
   )

to override default of not displaying multiple select widget for models with through option specified docs.

Although this works I still think that this should not cause error in Django.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文