• 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)
  • 相关阅读:
    SQLServer基本函数
    XP极限编程
    中日完全相同的四字成语
    SQL Server和Oracle的常用函数对比
    HTTP请求头概述
    XP(Extreme Programming)
    查看汉字各种编码值
    XP极限编程(中英文对照)
    http 头信息详解
    ASP内置对象详解
  • 原文地址:https://www.cnblogs.com/jingzaixin/p/13262263.html
Copyright © 2020-2023  润新知