zoukankan      html  css  js  c++  java
  • django 内联过滤

    class Author(models.Model):
        name = models.CharField(max_length=50)
        desc = models.CharField(max_length=50)
    
    class Book(models.Model):
        author = models.ForeignKey(Author)
        title= models.CharField(max_length=50)
    
    class BookPrio::
        author = models.ForeignKey(Author)
        book = models.ForeignKey(Book)
        prio = models.IntegerField()
     

    我没有看到任何简单的方法来访问已编辑的对象,因此我将formfield_for_foreignkey定义放在change_view中,并在view函数中分配了内联.

    class BookPrioInline(admin.TabularInline):
        model = BookPrio
    
    class AuthorAdmin(admin.ModelAdmin):
        inlines = (BookPrioInline,)
    
        def change_view(self, request, object_id, extra_context=None):
              def formfield_for_foreignkey(self, db_field, request, **kwargs):
                  if db_field.name == 'book':
                      kwargs['queryset'] = Book.objects.filter(author__id=object_id)
                  return super(ItemInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
    
              ItemInline.formfield_for_foreignkey = formfield_for_foreignkey
    
              self.inline_instances = [ItemInline(self.model, self.admin_site)]
    
              return super(AuthorAdmin, self).change_view(request, object_id,
                  extra_context=extra_context)
    
    
    admin.site.register(Author, AuthorAdmin)
  • 相关阅读:
    Python服务器开发三:Socket
    系统性能检测工具之lsof
    系统性能检测工具之iostat
    系统性能检测工具之vmstat
    系统性能检测工具之sar
    系统性能检测工具之ps
    系统性能检测工具之top
    Linux常用命令大全
    授权
    RMAN之REPORT命令
  • 原文地址:https://www.cnblogs.com/jingzaixin/p/13262263.html
Copyright © 2011-2022 走看看