0

I created a simple Django Webapp using django-neomodel integration. When trying to create a new book, at "http://localhost:8000/book/new" and after submitting it, I see the error as shown in the traceback.

I have searched online, and in most of the cases, the errors seem to be because of some typos in specifying the model name, or inadvertently usage of strings. I have double-checked for such reasons, but couldn't see any such issues in my code.

Also, I tried using Forms, by creating a form and giving a specifying form in the views, rather than model itself. But I see the same error in that case too.

models.py:

TITLE_MAX_LEN = 100
USERNAME_MAX_LEN = 25
NAME_MAX_LEN = 25

class Book(DjangoNode):
    custom_pk = UniqueIdProperty()
    title = StringProperty(max_length=TITLE_MAX_LEN, unique_index=True, required=True)
    description = StringProperty() 
    difficulty = IntegerProperty()
    importance = FloatProperty()

    class Meta:
        app_label = 'knowledge'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('book-detail', kwargs={'pk': self.custom_pk})

views.py

class BookCreateView(CreateView):
    model = Book
    fields = ['title', 'description']
    template_name = "knowledge/book_form.html"


class BookDetailView(DetailView):
    model = Book
    template_name = "knowledge/book_detail.html"

urls.py

urlpatterns = [
    path('book/new/', BookCreateView.as_view(), name='book-create'),
    path('book/<str:pk>/', BookDetailView.as_view(), name='book-detail'),
]

Traceback:

Traceback:

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/base.py" in dispatch
  97.         return handler(request, *args, **kwargs)

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/detail.py" in get
  106.         self.object = self.get_object()

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/detail.py" in get_object
  30.             queryset = self.get_queryset()

File "/Users/sam/code/website/django_env/lib/python3.7/site-packages/django/views/generic/detail.py" in get_queryset
  67.                 return self.model._default_manager.all()

Exception Type: AttributeError at /book/63038803d06f4270b1bf4a738eefe916/
Exception Value: type object 'Book' has no attribute '_default_manager'

Django documentation says that Django takes care of creating a default manager. Am not sure why this error is thrown with my code. Any hints appreciated. Please help. Thanks!

  • It's because neomodel doesn't contain a default manager which Django will use to query data on those views. NeoModel use `nodes` instead of `objects` as normal Model, that's why this happen – Toan Quoc Ho Oct 27 '19 at 04:06
  • Then how do I get rid of the error that am getting? Am wondering why Django is still looking for default manager, when I have specified "DjangoNode" as the base class. Is it because of using views? Anything I should change there? – Sampath Chanda Oct 27 '19 at 04:40
  • To get rid of this, I think you should create your own View instead of utilize provided views from Django. And inside your view, you have to make your own queries to interact with the data via [neomodel](https://neomodel.readthedocs.io/en/latest/) API – Toan Quoc Ho Oct 27 '19 at 04:53
  • Got it. Thanks a lot :) Any chance if there's a nice views API available that is compatible with neomodel? like the one that Django provides by default. – Sampath Chanda Oct 27 '19 at 05:00
  • I haven't use this before so I don't know if there is any library which support like Django does. I have some research but not found any items yet. – Toan Quoc Ho Oct 27 '19 at 05:12
  • Okay. thanks a lot for taking time on this. Greatly helpful, Highly appreciated :) – Sampath Chanda Oct 27 '19 at 05:16

0 Answers0