4

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 ?
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

1 Answers1

4

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,))
            )
    
Community
  • 1
  • 1
Ivan Kharlamov
  • 1,889
  • 2
  • 24
  • 33