Django

Code

Ticket #4667 (new)

Opened 1 year ago

Last modified 4 days ago

[newforms-admin] add edit_inline support for generic relations

Reported by: Honza Král <Honza.Kral@gmail.com> Assigned to: Honza_Kral
Milestone: 1.0 beta Component: Admin interface
Version: newforms-admin Keywords: nfa-someday edit_inline generic content_type sprintsept14
Cc: paltman@gmail.com, simon@quo.co.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com, prufrocks@gmail.com, smcoll@gmail.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 1

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

newforms-admin-5519-generic-edit-inline.patch (7.8 kB) - added by Honza Král <Honza.Kral@gmail.com> on 06/23/07 11:59:29.
rough version of the patch
newforms-admin-5519-generic-edit-inline-sprint14sep.patch (8.6 kB) - added by Honza_Kral on 09/14/07 10:47:17.
version matching django @6158
newforms-admin-6426-generic-edit-inline.patch (9.4 kB) - added by Honza_Kral on 10/02/07 11:15:05.
Matching django version 6426
newforms-admin-6477-generic-edit-inline.patch (9.8 kB) - added by Honza_Kral on 10/22/07 14:34:42.
Matching django version 6477
4667-added-custom-formset.diff (3.9 kB) - added by Honza_Kral on 11/11/07 14:34:06.
new two-part patch for django @6657 (first part)
4667-generic_edit_inline.diff (6.0 kB) - added by Honza_Kral on 11/11/07 14:34:26.
new two-part patch for django @6657 (second part)
4667-generic-edit-inline.patch (3.6 kB) - added by paltman on 06/29/08 22:25:59.
Cleaned up previous patch to work with existing code base (r7771).

Change History

06/23/07 11:59:29 changed by Honza Král <Honza.Kral@gmail.com>

  • attachment newforms-admin-5519-generic-edit-inline.patch added.

rough version of the patch

06/25/07 04:33:28 changed by ubernostrum

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

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.

06/25/07 04:33:42 changed by ubernostrum

  • needs_better_patch set to 1.

08/28/07 01:25:12 changed by anonymous

  • cc set to simon@quo.com.au.

09/10/07 04:19:14 changed by Honza_Kral

  • owner changed from nobody to Honza_Kral.

09/14/07 10:47:17 changed by Honza_Kral

  • attachment newforms-admin-5519-generic-edit-inline-sprint14sep.patch added.

version matching django @6158

09/14/07 11:34:55 changed 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.

09/14/07 11:36:40 changed by Øyvind Saltvik <oyvind@saltvik.no>

  • keywords changed from edit_inline generic content_type to edit_inline generic content_type sprintsept14.

09/14/07 11:49:18 changed by Honza_Kral

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.

09/14/07 13:48:13 changed by jkocherhans

  • stage changed from Unreviewed to Accepted.

10/02/07 11:15:05 changed by Honza_Kral

  • attachment newforms-admin-6426-generic-edit-inline.patch added.

Matching django version 6426

10/22/07 14:34:42 changed by Honza_Kral

  • attachment newforms-admin-6477-generic-edit-inline.patch added.

Matching django version 6477

10/22/07 14:40:45 changed by Honza_Kral

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

11/11/07 14:34:06 changed by Honza_Kral

  • attachment 4667-added-custom-formset.diff added.

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

11/11/07 14:34:26 changed by Honza_Kral

  • attachment 4667-generic_edit_inline.diff added.

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

11/11/07 14:35:08 changed by Honza_Kral

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

12/07/07 20:28:37 changed by brosner

  • keywords changed from edit_inline generic content_type sprintsept14 to nfa-someday edit_inline generic content_type sprintsept14.

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

01/23/08 16:52:25 changed by korpios

  • cc changed from simon@quo.com.au to simon@quo.com.au, korpios@korpios.com.

05/16/08 08:35:48 changed by Guilherme M. Gondim <semente@taurinus.org>

  • cc changed from simon@quo.com.au, korpios@korpios.com to simon@quo.com.au, korpios@korpios.com, semente@taurinus.org.

05/17/08 18:00:40 changed by peschler

  • cc changed from simon@quo.com.au, korpios@korpios.com, semente@taurinus.org to simon@quo.com.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com.

06/05/08 13:06:30 changed by prufrocks

  • cc changed from simon@quo.com.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com to simon@quo.com.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com, prufrocks@gmail.com.

06/18/08 13:50:51 changed by anonymous

  • cc changed from simon@quo.com.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com, prufrocks@gmail.com to simon@quo.co.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com, prufrocks@gmail.com, smcoll@gmail.com.

06/29/08 22:25:59 changed by paltman

  • attachment 4667-generic-edit-inline.patch added.

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

06/29/08 23:59:30 changed by paltman

  • cc changed from simon@quo.co.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com, prufrocks@gmail.com, smcoll@gmail.com to paltman@gmail.com, simon@quo.co.au, korpios@korpios.com, semente@taurinus.org, peschler@gmail.com, prufrocks@gmail.com, smcoll@gmail.com.

06/30/08 14:52:48 changed by brosner

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.


Add/Change #4667 ([newforms-admin] add edit_inline support for generic relations)




Change Properties
Action