Search code examples
djangopython-3.xdjango-file-uploaddjango-filebrowserdjango-2.1

Module 'django.db.models' has no attribute 'FileBrowseField'


I would like to have on my Django 2.1.1 site django-filebrowser-no-grappelli. I've followed this indications but at the end of the procedure, when I restart my server, I've this error:

header_image = models.FileBrowseField("Image", max_length=200, directory="images/", extensions=[".jpg"], blank=True) AttributeError: module 'django.db.models' has no attribute 'FileBrowseField'

This are the files of my project:

MODELS.PY

from django.db import models
from django.urls import reverse
from tinymce import HTMLField
from filebrowser.fields import FileBrowseField

class Post(models.Model):
    """definizione delle caratteristiche di un post"""
    title = models.CharField(max_length=70, help_text="Write post title here. The title must be have max 70 characters", verbose_name="Titolo", unique=True)
    short_description = models.TextField(max_length=200, help_text="Write a post short description here. The description must be have max 200 characters", verbose_name="Breve descrizione dell'articolo")
    contents = HTMLField(help_text="Write your post here", verbose_name="Contenuti")
    publishing_date = models.DateTimeField(verbose_name="Data di pubblicazione")
    updating_date = models.DateTimeField(auto_now=True, verbose_name="Data di aggiornamento")
    header_image = models.FileBrowseField("Image", max_length=200, directory="images/", extensions=[".jpg"], blank=True)
    slug = models.SlugField(verbose_name="Slug", unique="True", help_text="Slug is a field in autocomplete mode, but if you want you can modify its contents")

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("singlearticleView", kwargs={"slug": self.slug})

    class Meta:
        verbose_name = "Articolo"
        verbose_name_plural = "Articoli"

VIEWS.PY

from django.shortcuts import render
from .models import Post
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

class SingleArticle(DetailView):
    model = Post
    template_name = "single_article.html"


class ListArticles(ListView):
    model = Post
    template_name = "list_articles.html"

    def get_queryset(self):
        return Post.objects.order_by('-publishing_date')

URLS.PY

from django.urls import path
from .views import ListArticles, SingleArticle

urlpatterns = [
    path("", ListArticles.as_view(), name="listarticlesView"),
    path("<slug:slug>/", SingleArticle.as_view(), name="singlearticleView"),
]

ADMIN.PY

from django.contrib import admin
from .models import Post

class PostAdmin(admin.ModelAdmin):
    """creazione delle caratteristiche dei post leggibili nel pannello di amministrazione"""
    list_display = ["title", "publishing_date", "updating_date", "id"]
    list_filter = ["publishing_date"]
    search_fields = ["title", "short_description", "contents"]
    prepopulated_fields = {"slug": ("title",)}
    fieldsets = [
                (None, {"fields": ["title", "slug"]}),
                ("Contenuti", {"fields": ["short_description", "contents"]}),
                ("Riferimenti", {"fields": ["publishing_date"]}),
            ]

    class Meta:
        model = Post

admin.site.register(Post, PostAdmin)

URLS.PY PROJECT

from django.contrib import admin
from django.urls import path, include
from filebrowser.sites import site

urlpatterns = [
    path('admin/filebrowser/', site.urls),
    path('grappelli/', include('grappelli.urls')),
    path('admin/', admin.site.urls),
    path('', include('app4test.urls')),
    path('tinymce/', include('tinymce.urls')),
]

I've started all of this because I will use TinyMCE and a file browser application is necessary. When I deactive the string header_image in models.py the project running well but, obviously, when I try to upload an image I've an error.

Where I made a mistake?


Solution

  • your import is done like this:

    from filebrowser.fields import FileBrowseField
    

    but you're trying to use the field following way:

    header_image = models.FileBrowseField(...)
    

    It's not part of the models package, just do it without the models. part:

    header_image = FileBrowseField(...)