Opened 18 years ago

Closed 17 years ago

Last modified 17 years ago

#2248 closed enhancement (wontfix)

[patch] inline_models, replacement for edit_inline and core=True

Reported by: joaoma@… Owned by: nobody
Component: contrib.admin Version: dev
Severity: minor Keywords: edit_inline
Cc: tyson@…, jkocherhans@…, gary.wilson@…, karsten@…, ruckc@… Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I always found the edit_inline syntax quite strange. So, I decided to try something different: let the inline models be configured in the model itself, not in the children models. I came up with the given patch, which basically allows the use of inline models the following way:

class Person(models.Model):
    name = models.CharField(maxlength=20)
    
    class Admin:
        inline_models = (
            {'model':'Child',
             'type':models.TABULAR,
             'min_num_in_admin':3,
             'max_num_in_admin':20,
             'num_extra_on_change':3,
             'fields':('name','age',) 
            },
            {'model':'Job',
             'type':models.STACKED,
             'min_num_in_admin':1,
             'max_num_in_admin':3,
             'fields':('company','salary',) 
            }
        )

class Child(models.Model):
    parent = models.ForeignKey(Person)
    name = models.CharField(maxlength=100)
    age = models.IntegerField()
    hair_color = models.CharField(maxlength=20,blank=True,null=True)

class Job(models.Model):
    person = models.ForeignKey(Person)
    company = models.CharField(maxlength=100)
    salary = models.IntegerField()
    section = models.CharField(maxlength=50)

Attachments (3)

inline_models_in_Admin.diff (5.6 KB ) - added by joaoma@… 18 years ago.
patch for the new inline_models syntax
inline_models_in_Admin.2.diff (5.0 KB ) - added by JoaoJoao 18 years ago.
[patch] a *much* better patch for inline_models
inline_models_in_Admin.3.diff (5.0 KB ) - added by JoaoJoao 18 years ago.
Third patch, 'fields' isn't a good name for the core fields list, updated to 'core_fields'

Download all attachments as: .zip

Change History (19)

by joaoma@…, 18 years ago

Attachment: inline_models_in_Admin.diff added

patch for the new inline_models syntax

comment:1 by Adrian Holovaty, 18 years ago

Oooooh! I haven't looked at the patch, but the idea is fantastic.

comment:2 by anonymous, 18 years ago

Summary: Replacement for edit_inline and core=True[patch] inline_models, replacement for edit_inline and core=True

comment:3 by JoaoJoao, 18 years ago

I'm trying a decent patch, the one I attached is ugly.

by JoaoJoao, 18 years ago

[patch] a *much* better patch for inline_models

by JoaoJoao, 18 years ago

Third patch, 'fields' isn't a good name for the core fields list, updated to 'core_fields'

comment:4 by Tyson Tate <tyson@…>, 18 years ago

Cc: tyson@… added

I second this. I'd really like to see this implemented.

comment:5 by anonymous, 18 years ago

Cc: jkocherhans@… added

comment:6 by treborhudson@…, 18 years ago

Is it possible to add something related to the the ordering of multiple inline related tables? Using vanilla 0.95 I have 3 tables related to one model currently and there doesn't seem to be a reasoning to how they're pulled in (not alphabetical by table name or not the same as the order in the models.py file). Maybe simply the order you list them in the inline_models list would be enough to satisfy this.

I like this patch. I think it consolidates the features of the admin and cleans up the model code.

comment:7 by Gary Wilson <gary.wilson@…>, 18 years ago

Cc: gary.wilson@… added

comment:8 by Karsten W. Rohrbach <karsten@…>, 18 years ago

Cc: karsten@… added

comment:9 by ruckc@…, 18 years ago

Cc: ruckc@… added

comment:10 by (none), 18 years ago

milestone: Version 1.0

Milestone Version 1.0 deleted

comment:11 by Gary Wilson <gary.wilson@…>, 18 years ago

Needs tests: set
Triage Stage: UnreviewedDesign decision needed

Adrian has an alternative suggestion here: http://groups.google.com/group/django-developers/browse_thread/thread/704546f4b30d5c86/a4ead31d11b8a79a

that uses objects:

from django.contrib.admin import TabularInline, StackedInline

    # ...

    class Admin:
        inlines = (
            TabularInline('Child', min=3, max=20, extra=3,
                fields=('name', 'age')),
            StackedInline('Job', min=1, max=3,
                fields=('company', 'salary')),
        ) 

comment:12 by sansmojo@…, 18 years ago

I'm not sure if this is the best place for this, but another things that would be nice about inlines is to have an option to require at least one to be created. For example:

class Admin:

inline_models = (

{'model':'Child',

'type':models.TABULAR,
'min_num_in_admin':3,
'max_num_in_admin':20,
'num_extra_on_change':3,
'num_required': 1,
'fields':('name','age',)

},
{'model':'Job',

'type':models.STACKED,
'min_num_in_admin':1,
'max_num_in_admin':3,
'fields':('company','salary',)

}

)

would require at least one Child to be created.

comment:13 by joaoma@…, 18 years ago

Adrian's idea is definitely better than mine, as it's cleaner and more extensible. The original idea is much easier to implement, though.

comment:14 by philippe.raoult@…, 18 years ago

I've been looking at the problem you're trying to solve and I must say that while your patch would really solve it.

On the other hand, it feels like a hack. From the OO point of view, it means defining how the base class is going to be used in each child class inside the base class itself! I think all the options should be in the child class. I'm not familiar with Django's internals but it would be much more natural to have all this machinery defined in the foreign key. Someone (sorry, don't have a ref) suggested adding an "edit_inline_here" keyword in the foreign key, which would do the same thing.

While this might sound like nitpicking for the current example, it means to worlds when you're trying to design a modular app. If you have a Person class somewhere in your app and you want to extend it you should be able to do so without modifying the base class.

comment:15 by James Bennett, 17 years ago

Resolution: wontfix
Status: newclosed

This implementation was discarded in favor of the TabularInline and related classes in newforms-admin.

comment:16 by Antonis Christofides <anthony@…>, 17 years ago

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