Opened 18 years ago

Closed 16 years ago

Last modified 13 years ago

#4667 closed (fixed)

[newforms-admin] add edit_inline support for generic relations

Reported by: Honza Král <Honza.Kral@…> Owned by: Brian Rosner
Component: contrib.admin Version: dev
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
Easy pickings: no UI/UX: no

Description

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_field as 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.

Attachments (10)

newforms-admin-5519-generic-edit-inline.patch (7.8 KB ) - added by Honza Král <Honza.Kral@…> 18 years ago.
rough version of the patch
newforms-admin-5519-generic-edit-inline-sprint14sep.patch (8.6 KB ) - added by Honza Král 17 years ago.
version matching django @6158
newforms-admin-6426-generic-edit-inline.patch (9.4 KB ) - added by Honza Král 17 years ago.
Matching django version 6426
newforms-admin-6477-generic-edit-inline.patch (9.8 KB ) - added by Honza Král 17 years ago.
Matching django version 6477
4667-added-custom-formset.diff (3.9 KB ) - added by Honza Král 17 years ago.
new two-part patch for django @6657 (first part)
4667-generic_edit_inline.diff (6.0 KB ) - added by Honza Král 17 years ago.
new two-part patch for django @6657 (second part)
4667-generic-edit-inline.patch (3.6 KB ) - added by Patrick Altman 16 years ago.
Cleaned up previous patch to work with existing code base (r7771).
4667-against-7875.patch (3.7 KB ) - added by Honza Král 16 years ago.
4667-against-7875.2.patch (3.7 KB ) - added by Jonas Fiala 16 years ago.
Patch modified - save_as_new argument missing in init
4667-against-7875.3.patch (3.7 KB ) - added by jakub_vysoky 16 years ago.
can_delete and can_order update for the patch

Download all attachments as: .zip

Change History (38)

by Honza Král <Honza.Kral@…>, 18 years ago

rough version of the patch

comment:1 by James Bennett, 18 years ago

Just a minor style note: the preferred style for Django patches is to do function(arg), not function( arg ), following the lead of PEP 8.

comment:2 by James Bennett, 18 years ago

Patch needs improvement: set

comment:3 by anonymous, 17 years ago

Cc: simon@… added

comment:4 by Honza Král, 17 years ago

Owner: changed from nobody to Honza Král

by Honza Král, 17 years ago

version matching django @6158

comment:5 by jkocherhans, 17 years ago

So first of all, the name='object_id:content_type' syntax is just too dirty. It seems like we should be able to provide *just* the name of the GenericForeignKey field, then use its ct_field and fk_field attributes to get what we need. If that isn't possible, I'd much rather use 2 different arguments, something like ct_field and fk_field.

Also, I *think* we could use the existing FormSet.add_fields hook instead of adding a new add_fk method like in the patch.

Keep in mind that I may be missing something here. These are my initial impressions, but they may not end up working in practice. I haven't worked all the way through it.

comment:6 by Øyvind Saltvik <oyvind@…>, 17 years ago

Keywords: sprintsept14 added

comment:7 by Honza Král, 17 years ago

Replying to jkocherhans:

So first of all, the name='object_id:content_type' syntax is just too dirty. It seems like we should be able to provide *just* the name of the GenericForeignKey field, then use its ct_field and fk_field attributes to get what we need. If that isn't possible, I'd much rather use 2 different arguments, something like ct_field and fk_field.

I dont want that because I often don't have GenericForeignKey defined - it is a modular system and I do not want to modify every model that can use the generic relation. so for that I would have to override the InlineModelAdmin as well to provide two options - ct_name and id_name, but that's certainly an option...

Also, I *think* we could use the existing FormSet.add_fields hook instead of adding a new add_fk method like in the patch.

The reason for this is that the patch existed before that hooks were added, I just did a small update to match the current version... I will have a look at that


Keep in mind that I may be missing something here. These are my initial impressions, but they may not end up working in practice. I haven't worked all the way through it.

comment:8 by jkocherhans, 17 years ago

Triage Stage: UnreviewedAccepted

by Honza Král, 17 years ago

Matching django version 6426

by Honza Král, 17 years ago

Matching django version 6477

comment:9 by Honza Král, 17 years ago

I moved all the code except few things (enable formset overriding and something to prevent circular imports) out of admin and directly into contenttypes.generic

This will enable me to specify my generic inlines like

class ListingInlineOptions( generic.GenericTabularInline ): 
    model = Listing           
    extra = 2                    
    ct_field_name = 'target_ct'                                                                                                                                           
    id_field_name = 'target_id'

The downside is that I am duplicating some code in contenttypes.generic

There are few more things to do:

  1. Find better names than id_field_name and ct_field_name
  2. Enable this to work with autodetection and/or GenericForeignKey
  3. clean-up the code in contenttypes.generic (maybe refactor admin a bit to enable getting rid of some boilerplate code)
  4. Enjoy :)

by Honza Král, 17 years ago

new two-part patch for django @6657 (first part)

by Honza Král, 17 years ago

new two-part patch for django @6657 (second part)

comment:10 by Honza Král, 17 years ago

I split the patch into two parts, one handling the custom formset overrides, the second actually implementing the feature.

comment:11 by Brian Rosner, 17 years ago

Keywords: nfa-someday added

This functionality is not critical before the merge to trunk. Tagging with nfa-someday.

comment:12 by korpios, 17 years ago

Cc: korpios@… added

comment:13 by Guilherme M. Gondim <semente@…>, 17 years ago

Cc: semente@… added

comment:14 by peschler, 17 years ago

Cc: peschler@… added

comment:15 by prufrocks, 17 years ago

Cc: prufrocks@… added

comment:16 by anonymous, 17 years ago

Cc: simon@… smcoll@… added; simon@… removed

by Patrick Altman, 16 years ago

Cleaned up previous patch to work with existing code base (r7771).

comment:17 by Patrick Altman, 16 years ago

Cc: paltman@… added

comment:18 by Brian Rosner, 16 years ago

The latest patch has the code in django/contrib/admin/options.py which is a wrong place for the code. It should have a nice home in django/contrib/contenttypes. Not sure where but I am thinking either inlines.py or admin.py.

by Honza Král, 16 years ago

Attachment: 4667-against-7875.patch added

comment:19 by Honza Král, 16 years ago

Keywords: ep2008 added

by Jonas Fiala, 16 years ago

Attachment: 4667-against-7875.2.patch added

Patch modified - save_as_new argument missing in init

comment:20 by Jonas Fiala, 16 years ago

Modified constructor (added save_as_new argument), patch 4667-against-7875.2.patch works well on rev. 7877, all tests passed.

Please change your admin options if using example at the top of this page:

class SomeInline( generic.GenericTabularInline ):
    model = Dependency
    ct_field_name = 'source_ct'
    id_field_name = 'target_id'

class SomeModelOptions( admin.ModelAdmin ):
    inlines = [ SomeInline ]

by jakub_vysoky, 16 years ago

Attachment: 4667-against-7875.3.patch added

can_delete and can_order update for the patch

comment:21 by jakub_vysoky, 16 years ago

in my last attachment I just added same default values for can_delete and can_order like there are in django.newforms.models.inlineformset_factory

comment:22 by jakub_vysoky, 16 years ago

[source:django/branches/newforms-admin/django/newforms/models.py@7899#L450 django.newforms.models.inlineformset_factory]

comment:23 by anonymous, 16 years ago

Cc: dev@… added

comment:24 by Alex Gaynor, 16 years ago

Version: newforms-adminSVN

comment:25 by Brian Rosner, 16 years ago

Owner: changed from Honza Král to Brian Rosner
Status: newassigned

comment:26 by Brian Rosner, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [8279]) Fixed #4667 -- Added support for inline generic relations in the admin. Thanks to Honza Král and Alex Gaynor for their work on this ticket.

comment:27 by korpios, 16 years ago

Cc: korpios@… removed

comment:28 by Jacob, 13 years ago

milestone: 1.0 beta

Milestone 1.0 beta deleted

Note: See TracTickets for help on using tickets.
Back to Top