Django - 管理员:在没有 inlineForm 的情况下编辑子模型

发布于 2024-12-14 11:11:52 字数 974 浏览 1 评论 0原文

标准示例:

class Author(models.Model):
  name = models.CharField(max_length=100)

class Book(models.Model):
  title = models.CharField(max_length=100)
  author = models.ForeignKey(Author)
  #... Many other fields ...

我想从作者更改页面编辑书籍
我尝试使用 InlineModelAdmin 但自从 Book 字段较多,不易编辑。
这就是为什么我尝试在作者/更改模板上添加指向孩子的链接。

<ul>
  <li><a href="{% url admin:content_scribpart_add %}">Add a Book</a></li>
{% for book in original.book_set.all %}
  <li><a href="{% url admin:myapp_book_change book.id %}">Edit {{ book }}</a></li>
{% endfor %}
</ul>

但有几个问题

  • 我如何在 Book 表单中预填充相关的 Author id
  • 如何让保存按钮返回到相关 作者
  • 我走在正确的道路上吗?

Standard example:

class Author(models.Model):
  name = models.CharField(max_length=100)

class Book(models.Model):
  title = models.CharField(max_length=100)
  author = models.ForeignKey(Author)
  #... Many other fields ...

I'd like to edit the Books from the Author change page.
I tried with InlineModelAdmin but since Book has many fields, it's not easy to edit.
That's why I tried to put links towards the children on the author/change template.

<ul>
  <li><a href="{% url admin:content_scribpart_add %}">Add a Book</a></li>
{% for book in original.book_set.all %}
  <li><a href="{% url admin:myapp_book_change book.id %}">Edit {{ book }}</a></li>
{% endfor %}
</ul>

But there are several questions

  • How can I prepopulate the related Author id in the Book form
  • How can I make the Save button to go back to the related Author
  • Am I on right track ?

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

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

发布评论

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

评论(1

梦晓ヶ微光ヅ倾城 2024-12-21 11:11:52

是的,当然。

  1. author 主键作为 GET 参数附加到您的网址:

    <前><代码>

  2. 修改图书相应的 ModealAdmin,覆盖 response_add()response_change()。请注意,我们还 覆盖 formfield_for_forein_key 以便预先填充 author 字段:

    from django.http import HttpResponseRedirect
    从 django.core.urlresolvers 导入反向
    
    类 BookAdmin(admin.ModelAdmin):
    
        def formfield_for_foreignkey(self, db_field, request, **kwargs):
            如果 db_field.name == "作者":                    
                尝试:
                    author_pk = int(request.GET.get('作者', ''),) 
                除了值错误:           
                    经过
                别的:
                    kwargs["初始"] = Author.objects.get(pk=author_pk)
    
            返回 super(BookAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
    
        def response_add(self, request, obj, post_url_continue=None):
            返回 HttpResponseRedirect(reverse('admin:myapp_author_change', args=(obj.author.pk,))
            )
    
        def response_change(self, request, obj, post_url_continue=None):
            返回 HttpResponseRedirect(reverse('admin:myapp_author_change', args=(obj.author.pk,))
            )
    

Yes, sure.

  1. Append author primary key as GET parameter to your url:

    <ul>
      <li><a href="{% url admin:content_scribpart_add %}?author={{ object_id }}">Add a Book</a></li>
    {% for book in original.book_set.all %}
      <li><a href="{% url admin:myapp_book_change book.id %}?author={{ object_id }}">Edit {{ book }}</a></li>
    {% endfor %}
    </ul>
    
  2. Modify the corresponding ModealAdmin for book, override response_add() and response_change(). Note that we also override formfield_for_forein_key in order to pre-populate author field:

    from django.http import HttpResponseRedirect
    from django.core.urlresolvers import reverse
    
    class BookAdmin(admin.ModelAdmin):
    
        def formfield_for_foreignkey(self, db_field, request, **kwargs):
            if db_field.name == "author":                    
                try:
                    author_pk = int(request.GET.get('author', ''),) 
                except ValueError:           
                    pass
                else:
                    kwargs["initial"] = Author.objects.get(pk=author_pk)
    
            return super(BookAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
    
        def response_add(self, request, obj, post_url_continue=None):
            return HttpResponseRedirect(reverse('admin:myapp_author_change', args=(obj.author.pk,))
            )
    
        def response_change(self, request, obj, post_url_continue=None):
            return HttpResponseRedirect(reverse('admin:myapp_author_change', args=(obj.author.pk,))
            )
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文