Opened 16 years ago

Closed 15 years ago

Last modified 12 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@…> 16 years ago.
rough version of the patch
newforms-admin-5519-generic-edit-inline-sprint14sep.patch (8.6 KB) - added by Honza Král 16 years ago.
version matching django @6158
newforms-admin-6426-generic-edit-inline.patch (9.4 KB) - added by Honza Král 16 years ago.
Matching django version 6426
newforms-admin-6477-generic-edit-inline.patch (9.8 KB) - added by Honza Král 16 years ago.
Matching django version 6477
4667-added-custom-formset.diff (3.9 KB) - added by Honza Král 16 years ago.
new two-part patch for django @6657 (first part)
4667-generic_edit_inline.diff (6.0 KB) - added by Honza Král 16 years ago.
new two-part patch for django @6657 (second part)
4667-generic-edit-inline.patch (3.6 KB) - added by paltman 15 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 15 years ago.
4667-against-7875.2.patch (3.7 KB) - added by Jonas Fiala 15 years ago.
Patch modified - save_as_new argument missing in init
4667-against-7875.3.patch (3.7 KB) - added by jakub_vysoky 15 years ago.
can_delete and can_order update for the patch

Download all attachments as: .zip

Change History (38)

Changed 16 years ago by Honza Král <Honza.Kral@…>

rough version of the patch

comment:1 Changed 16 years ago by James Bennett

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 Changed 16 years ago by James Bennett

Patch needs improvement: set

comment:3 Changed 16 years ago by anonymous

Cc: simon@… added

comment:4 Changed 16 years ago by Honza Král

Owner: changed from nobody to Honza Král

Changed 16 years ago by Honza Král

version matching django @6158

comment:5 Changed 16 years ago by 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.

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 Changed 16 years ago by Øyvind Saltvik <oyvind@…>

Keywords: sprintsept14 added

comment:7 Changed 16 years ago by Honza Král

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 Changed 16 years ago by jkocherhans

Triage Stage: UnreviewedAccepted

Changed 16 years ago by Honza Král

Matching django version 6426

Changed 16 years ago by Honza Král

Matching django version 6477

comment:9 Changed 16 years ago by Honza Král

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 :)

Changed 16 years ago by Honza Král

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

Changed 16 years ago by Honza Král

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

comment:10 Changed 16 years ago by Honza Král

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

comment:11 Changed 16 years ago by Brian Rosner

Keywords: nfa-someday added

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

comment:12 Changed 16 years ago by korpios

Cc: korpios@… added

comment:13 Changed 15 years ago by Guilherme M. Gondim <semente@…>

Cc: semente@… added

comment:14 Changed 15 years ago by peschler

Cc: peschler@… added

comment:15 Changed 15 years ago by prufrocks

Cc: prufrocks@… added

comment:16 Changed 15 years ago by anonymous

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

Changed 15 years ago by paltman

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

comment:17 Changed 15 years ago by paltman

Cc: paltman@… added

comment:18 Changed 15 years ago by Brian Rosner

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.

Changed 15 years ago by Honza Král

Attachment: 4667-against-7875.patch added

comment:19 Changed 15 years ago by Honza Král

Keywords: ep2008 added

Changed 15 years ago by Jonas Fiala

Attachment: 4667-against-7875.2.patch added

Patch modified - save_as_new argument missing in init

comment:20 Changed 15 years ago by Jonas Fiala

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 ]

Changed 15 years ago by jakub_vysoky

Attachment: 4667-against-7875.3.patch added

can_delete and can_order update for the patch

comment:21 Changed 15 years ago by jakub_vysoky

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 Changed 15 years ago by jakub_vysoky

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

comment:23 Changed 15 years ago by anonymous

Cc: dev@… added

comment:24 Changed 15 years ago by Alex Gaynor

Version: newforms-adminSVN

comment:25 Changed 15 years ago by Brian Rosner

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

comment:26 Changed 15 years ago by Brian Rosner

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 Changed 15 years ago by korpios

Cc: korpios@… removed

comment:28 Changed 12 years ago by Jacob

milestone: 1.0 beta

Milestone 1.0 beta deleted

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