jrief / django-admin-sortable2

Generic drag-and-drop ordering for objects in the Django admin interface
https://django-admin-sortable2.readthedocs.io/en/latest/
Other
743 stars 179 forks source link

Error with GenericTabularInline #153

Open ar7n opened 7 years ago

ar7n commented 7 years ago

Hello, I have this error trying to use django-admin-sortable2 with GenericTabularInline

AttributeError at /admin/news/news/1/change/
type object 'ImageFormFormSet' has no attribute 'fk'
Request Method: GET
Request URL:    http://localhost:8000/admin/news/news/1/change/
Django Version: 1.11.1
Exception Type: AttributeError
Exception Value:    
type object 'ImageFormFormSet' has no attribute 'fk'
Exception Location: /Users/arseny/www/villagio-realty/env/lib/python3.6/site-packages/django/forms/models.py in get_default_prefix, line 919
Python Executable:  /Users/arseny/www/villagio-realty/env/bin/python
Python Version: 3.6.1
Python Path:    
['/Users/arseny/www/villagio-realty/villagiorealty',
 '/Users/arseny/www/villagio-realty/villagiorealty',
 '/Users/arseny/www/villagio-realty',
 '/Users/arseny/www/villagio-realty/env/lib/python36.zip',
 '/Users/arseny/www/villagio-realty/env/lib/python3.6',
 '/Users/arseny/www/villagio-realty/env/lib/python3.6/lib-dynload',
 '/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6',
 '/Users/arseny/www/villagio-realty/env/lib/python3.6/site-packages']
Server time:    Пт, 19 Май 2017 16:13:37 +0300`

models.py

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from adminsortable.models import SortableMixin

class Image(SortableMixin):
    file = models.ImageField('Файл')
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
    object_id = models.PositiveIntegerField(null=True)
    content_object = GenericForeignKey('content_type', 'object_id')
    position = models.IntegerField('Позиция', default=0)

    def __str__(self):
        return self.file.url

    class Meta:
        verbose_name = 'Изображение'
        verbose_name_plural = 'Изображения'
        ordering = ('position',)

admin.py

from django.contrib import admin
from django.contrib.contenttypes.admin import GenericTabularInline
from adminsortable2.admin import SortableInlineAdminMixin
from .models import News
from ..image.models import Image

class ImageInline(SortableInlineAdminMixin, GenericTabularInline):
    model = Image

class NewsAdmin(admin.ModelAdmin):
    inlines = (
        ImageInline,
    )

admin.site.register(News, NewsAdmin)
jrief commented 7 years ago

In class Image(SortableMixin): where is your models.Model?

ar7n commented 7 years ago

This version with models.Model has same error

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from imagekit.models import ImageSpecField
from imagekit.processors import Thumbnail
from model_utils.models import TimeStampedModel

class Image(TimeStampedModel, models.Model):
    file = models.ImageField('Файл')
    thumbnail = ImageSpecField(
        source='file',
        processors=[Thumbnail(100, 100, crop=True, upscale=True)])
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
    object_id = models.PositiveIntegerField(null=True)
    content_object = GenericForeignKey('content_type', 'object_id')
    order = models.PositiveIntegerField(default=0, editable=False, db_index=True)

    def __str__(self):
        return self.file.url

    class Meta:
        verbose_name = 'Изображение'
        verbose_name_plural = 'Изображения'
        ordering = ['order']
LorisLunedei commented 7 years ago

I have the same problem:

AttributeError at /admin/news/news/7/change/

type object 'DownloadFormFormSet' has no attribute 'fk'

Request Method:     GET
Request URL:    http://127.0.0.1:8000/admin/news/news/7/change/
Django Version:     1.11.1
Exception Type:     AttributeError
Exception Value:    

type object 'DownloadFormFormSet' has no attribute 'fk'

Exception Location:     /home/vagrant/.pyenv/versions/backoffice.sagreefeste.local.it/lib/python3.6/site-packages/django/forms/models.py in get_default_prefix, line 919
Python Executable:  /home/vagrant/.pyenv/versions/backoffice.sagreefeste.local.it/bin/python
Python Version:     3.6.0
Python Path:    

['/vagrant',
 '/home/vagrant/.pyenv/versions/3.6.0/lib/python36.zip',
 '/home/vagrant/.pyenv/versions/3.6.0/lib/python3.6',
 '/home/vagrant/.pyenv/versions/3.6.0/lib/python3.6/lib-dynload',
 '/home/vagrant/.pyenv/versions/backoffice.sagreefeste.local.it/lib/python3.6/site-packages']

Server time:    Lun, 29 Mag 2017 12:19:27 +0000

models.py

from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.utils.translation import gettext_lazy as _
from model_utils.models import TimeStampedModel

class Download(TimeStampedModel, models.Model):
    name = models.CharField(max_length=500, null=False, blank=False, verbose_name=_("Nome"))
    file = models.FileField(null=False, blank=False, upload_to="downloads/", verbose_name=_("File"))
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')
    order = models.PositiveIntegerField(
        default=0, verbose_name=_('Ordinamento'), blank=False, null=False)  # default 0 per il sorting

    class Meta:
        verbose_name_plural = _("Downloads")
        ordering = ('order',)

admin.py

...
from django.contrib.contenttypes.admin import GenericTabularInline
from adminsortable2.admin import SortableInlineAdminMixin

class DownloadInline(SortableInlineAdminMixin, GenericTabularInline):
    model = Download
    extra = 0
    list_display = ('name', 'file',)
    verbose_name_plural = 'Downloads'

class NewsAdmin(admin.ModelAdmin):
    readonly_fields = ['thumb']
    search_fields = ['text', ]
    list_display = ['title', 'visible', ]
    save_on_top = True
    inlines = [DownloadInline, ]

admin.site.register(News, NewsAdmin)
LorisLunedei commented 7 years ago

There is some custom implementation for GenericTabularInline?

makaleks commented 5 years ago

Same problem, disappears when SortableInlineAdminMixin is not used.