specialunderwear / django-easymode

Quickly build backends for flash/flex websites with Django.
GNU General Public License v3.0
35 stars 20 forks source link

Incorrect LinkInline behaviour #3

Open nnseva opened 13 years ago

nnseva commented 13 years ago

For some reason, the LinkInline doesn't show inline objects except the first one.

I've created a very simple example with two models:

from django.db import models

# Create your models here.
class Top(models.Model):
    text = models.TextField()
class Bottom(models.Model):
    top = models.ForeignKey(Top)
    text = models.TextField()

and created a simple admin for them:

from django.contrib import admin
from django.conf.urls.defaults import *

from easymode.tree.admin.abstract import LinkInline, LinkedItemAdmin
from easymode.tree.admin.relation import InvisibleModelAdmin

from testone import models

class BottomInline(LinkInline):
        model = models.Bottom
        parent_link = 'top'

class BottomAdmin(InvisibleModelAdmin):
        model = models.Bottom
        parent_link = 'top'

admin.site.register(models.Bottom,BottomAdmin)

class TopAdmin(LinkedItemAdmin):
        inlines = [
            BottomInline,
        ]

admin.site.register(models.Top,TopAdmin)

'''
class BottomInline(admin.TabularInline):
        model = models.Bottom

class TopAdmin(admin.ModelAdmin):
        inlines = [
            BottomInline,
        ]

admin.site.register(models.Top,TopAdmin)
'''

The first (uncommented) portion of the admin is working with easymode, while the second one (commented out) is a standard django admin code.

I am filling in one Top object and several Bottom objects linked to the Top one. When I am looking to the standard admin, I see several filled objects: https://docs.google.com/open?id=0B6V0N4fDeXFPNjJjZGExMDMtNjk5Yi00NzZiLWE2MjctZDVjNjE5OWRlNDM4

When I am looking to the easymode-powered admin, I see the only one Bottom object filled, but several object lines with links (links are working): https://docs.google.com/open?id=0B6V0N4fDeXFPYzM5MGZlMGItNjExYi00MGRhLTkwMjUtYjEwZDViZGEwNTU5

specialunderwear commented 13 years ago

Ok, yeah what you need to do is, define the fields property on the admin for the BottomInline. You should only include the foreign key. In your case it should look like:

class BottomInline(LinkInline):
    model = models.Bottom
    fields = ('top',)

The parent_link should not be used in the BottomInline, only in the BottomAdmin. http://packages.python.org/django-easymode/tree/api.html#easymode.tree.admin.abstract.LinkInline I don;t know why the fields are empty. That is a bug. Please try again with the definition of BottomInline as follows:

class BottomInline(LinkInline):
    model = models.Bottom
    fields = ('top', 'text')

But again, you should not show the fields in that form, only the links should be visible. So only the foreign key should be in fields

specialunderwear commented 13 years ago

The documentation also says you MUST define the fields property

class BottomLinkInline(LinkInline):
    """
    This is the inline view of the inlined item. It will de rendered as a
    link to the change view or add view of the inlined item.

    NOTE that you MUST define fields, which must only include the foreign key.
    Ofcourse you might want to include some more fields and put them as
    read_only_fields, to give a bit more info.
    """
    fields = ('top',)
    model = BottomModel
nnseva commented 13 years ago

Explicit definition of the 'fields' property doesn't help.

Definitely, the BottomInline works as described if I include the only 'top' field (foreign key) to the 'fields' property, but this is not an expected behaviour for the inline form inherited from the standard django inline :(

Really I wanted to have a standard django inline (probably readonly) with additional link to the full-featured (sub)object editor. The str() representation of the (sub)object helps partially, but is not a complete solution if I f.e. wanted to have a table of (sub)object rows. I will think about it ...

specialunderwear commented 13 years ago

If you add 'text' to readonly_fields does that work? Or is it still empty?

class BottomInline(LinkInline): model = models.Bottom fields = ('top', ) readonly_fields = ('text',)

nnseva commented 13 years ago

It doesn't work (the 'text' is not shown at all) in this form, but the following is working as expected:


class BottomInline(LinkInline):
        model = models.Bottom
        parent_link = 'top'
        fields = ('top','text')
        readonly_fields = ('text',)
specialunderwear commented 13 years ago

Ok so the documentation should be updated to reflect this.

nnseva commented 13 years ago

It might be useful to have a separate object like 'object link widget' which I can add to the standard (or inherited from standard) tabular or stacked inline ... this solution will cover all needs.

specialunderwear commented 13 years ago

Hmm the problem is, that django overrides every attempt to have a custom widget for a the foreign key field in inlines. That is why LinkInline has a custom formset, to stop django from resetting it to it's default.

See http://stackoverflow.com/questions/896153/how-to-show-hidden-autofield-in-django-formset/1266682#1266682

So what you are proposing is not possible. You always have to override the formset. Maybe what you want is what I'm doing in: https://github.com/specialunderwear/django-polymorphic-easymode/blob/master/mixed/admin.py

It also renders a link to the 'full' admin.

nnseva commented 13 years ago

Hmm ... it is really near to my needs, including polymorphing, I should investigate it deeply ...