Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot list all of my fields in list_editable without causing errors

I am trying to use list_editable to make all my fields editable on the same page. But unless I also have something in list_display_links I get errors. The problems I don't have any unused fields to put there. I am probably misunderstanding a concept somewhere.

What I have done is create a 'dummy' field in the model: dummy = None. This is not only clunky and probably wrong - but it also causes the dummy field to appear in my admin.

What am I doing wrong? I tried reading the docs but I can't find the solution to my problem. I would like to go about this the "right way", whatever that may be.

Here is my code:

models.py

...

class Slider(models.Model):
    slider_title = models.CharField(max_length=20)
    slider_text = models.TextField(max_length=200)
    slider_order = models.PositiveSmallIntegerField(
        default=1, blank=True, null=True, choices=[(1, 'first'),
                                                   (2, 'middle'), (3, 'last')])
    dummy = None

    def clean(self):
        validate_only_three_instances(self)

    def __str__(self):
        return self.slider_title

...

admin.py

...

class SliderAdmin(admin.ModelAdmin):

    # remove "add" button
    def has_add_permission(self, request):
        return False

    fieldsets = [
        (None,  {'fields': ['slider_title']}),
        (None,  {'fields': ['slider_text']}),
        (None,  {'fields': ['slider_order']}),
    ]

    list_display = (
        'slider_title', 'slider_text', 'slider_order', 'dummy',)
    list_display_links = ('dummy',)
    list_editable = ('slider_title', 'slider_text', 'slider_order',)

...
like image 286
user1026169 Avatar asked Mar 04 '14 22:03

user1026169


1 Answers

I understand now. For some reason the official documentation didn't click with me, however reading this did: http://django-suit.readthedocs.org/en/latest/sortables.html

To sum things up:

list_display - is for which fields will appear in the admin page

list_editable - is for which fields can be edited without officially opening them up in the "edit" page. you can basically just edit them right there on the spot in-line. pretty awesome.

list_display_links - at least one item from list_display must serve as the link to the edit page. this item can't also be in list_editable otherwise it couldn't serve as a link. (facepalm)

This is how I ended up modifying my files:

models.py

class Slider(models.Model):
    ...
    link = "Edit"
    ...

admin.py

class SliderAdmin(admin.ModelAdmin):
    ...
    list_display = (
        'slider_title', 'slider_text', 'slider_order', 'link',)
    list_display_links = ('link',)
    list_editable = ('slider_title', 'slider_text', 'slider_order',)
    ...
like image 189
user1026169 Avatar answered Oct 21 '22 21:10

user1026169