[newforms-admin] add edit_inline support for generic relations
|Reported by:||Owned by:||Brian Rosner|
|Severity:||Keywords:||nfa-someday edit_inline generic content_type sprintsept14 ep2008|
|Cc:||paltman@…, simon@…, semente@…, peschler@…, prufrocks@…, smcoll@…, dev@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
I have a model that looks like (working example)
from django.db import models from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType class Dependency( models.Model ): target_ct = models.ForeignKey( ContentType, related_name='dependency_for_set' ) target_id = models.IntegerField() target = generic.GenericForeignKey( 'target_ct', 'target_id' ) source_ct = models.ForeignKey( ContentType, related_name='dependent_on_set' ) source_id = models.IntegerField() source = generic.GenericForeignKey( 'source_ct', 'source_id' )
and I want to be able to edit it inline with other models, but the current implementations only support edit_inline for foreign key relations. With the attached patch I can do something like this:
from django.contrib import admin class SomeModel( models.Model ): text = models.TextField() class SomeModelOptions( admin.ModelAdmin ): inlines = [ admin.TabularInline( Dependency, name='source_ct:source_id', formset=generic.GenericInlineFormset ), ] admin.site.register( SomeModel , SomeModelOptions )
The patch is very rough, but working (tested adding and editing).
I have some questions for the authors (I will post it to django-dev):
- InlineFormset.rel_name is not really applicable in this case, what would be best to use as a prefix (the value in tha patch -
'aaa'is far from ideal ;) )
- I moved some of the functionality to FormSet (add_fk), to allow for custom binding between objects and use
content_type_field:object_id_fieldas the foreign key name to pass that information. I feel that its not the most elegant solution, could anybody help me out here with some ideas?
btw. Great work, Joseph Kocherhans, your implementation of edit_inline really made this easy for me, thanks.
Change History (38)
Changed 9 years ago by
comment:25 Changed 9 years ago by
|Owner:||changed from Honza Král to Brian Rosner|
|Status:||new → assigned|