ascending=True not working in django-mptt - django

Hi good day I'm currently following Django-MPTT documentation and I have a problem using ascending=True. Here's my code:
views.py
def show_genres(request):
Try01 = Genre.objects.filter(name="Rock")
context = {'genres': Genre.objects.all(),
'sample_ancestor': Try01.get_ancestors(ascending=True, include_self=True)}
return render(request, "sampletemp/startup.html", context)
when I'm using ascending=True an error occurs saying:
Exception Value: get_queryset_ancestors() got an unexpected keyword argument 'ascending'
How can I fix it. Thank you in advance!

You are using the wrong method
models.get_ancestors has an ascending field see here
managers.TreeManager.get_queryset_ancestors howerver has not, as shown here

Related

Why do I get an unexpected keyword argument?

I have created a model in my Djano project and the name of the model is Questions in which I have created a primary key called questionid. I am able to get all listings as summaries on one page however when I try to get a detailed listing of one query (say question number 4 out of the several in the table) by going to the http://127.0.0.1:8000/qanda/4 (here 4 is the question number), I get an error that says
TypeError at /qanda/4
question() got an unexpected keyword argument 'question_questionid'
Please see the code below
In my model's views file
def question(request):
questionsm = Questions.objects.order_by('-published_date').filter(is_published=True)
context = {
'questionid': questionsm
}
return render(request,'qanda/question.html', context)
In my model url file
path('<int:question_questionid>', views.question, name='question'),
I will appreciate any help. thanks
You are not passing the var to the view.
def question(request, **id_what_you_want**):
# and if you want an individual view, you should get by id
Questions.objects.get(pk=id_what_you_want)

Django query params to array

I am trying to get the results from an array as explained here: https://docs.djangoproject.com/en/dev/ref/models/querysets/#in
http://127.0.0.1:8000/blogs?years=['2018', '2019']
http://127.0.0.1:8000/blogs?years=[2018, 2019]
Turns out ['2018', '2019'] is not what i am getting as years , even though visually they look exactly the same.
I even tried using getlist as explained here how to get multiple results using same query params django this does produce the desired results
def get_queryset(self):
years = self.request.query_params.get('years')
return self.queryset.filter(year__in=years)
Any clue on what am i doing wrong here?
I tried all options it does not work, while i type in the below statement it works perfectly fine
def get_queryset(self):
return self.queryset.filter(year__in=['2018', '2019'])
I don't believe this way will work. When you pass the value in querystring, I guess Django will recieve as a string.
- EDIT -
The above answer wont work is need to add more years, I got the solution 127.0.0.1:8000/blogs?years=2018&years=2019 and
years = self.request.query_params.getlist('years', '')
converts this to list.
– #indavinci
I think a better a better and cleaner way to solve this problem is to just pass the string as comma-separated values in query params and split them up in your Django View.
URL:
http://127.0.0.1:8000/blogs?years=2018,2019
Your View:
def get_queryset(self):
years = self.request.query_params.get('years').split(',')
return self.queryset.filter(year__in=years)
If you have function based view this should work:
request.GET.getlist('years', '')

haystack SearchQuerySet() is returning list instead of SearchQuerySet object

this is my get_queryset(): method in the view
def get_queryset(self):
#by here the search query is getting executed
self.vendor_filter=self.request.GET.get('select_vendor', 'all')
self.search_query=self.request.GET.get('q', "")
self.sort_by=self.request.GET.get('sort_by', "relevance")
queryset=SearchQuerySet().all()[:50]
return queryset
this method is throwing 'list' object has no attribute 'all' error. However I ran this SearchQuerySet().all() in django shell it is returning correct results.
.
it is very annoying issue. I dont know what is the mistake?, I am using whoosh with django-haystack search.
I found the reason why, the code queryset=SearchQuerySet().all()[:50] was converting the SearchQuerySet object to list. I changed it to queryset=SearchQuerySet().all() now it works as expected. It took me whole day to figure out. But still I don't know why it was doing that without throwing an exception or error.

Django NoReverseMatch at

I have the following URLs:
url(r'^%sdform/' %(URLPREFIX), pv.dform, name='dform'),
url(r'^%sform/(P?<growl>.*)/' %(URLPREFIX), pv.dform, name='dform'),
The view code:
def dform(request, growl = None) is the method signature
The redirect code:
msg = 'test'
return redirect('dform', growl=msg)
Any idea why I get that error? I'm sending the right parameter to the right view method with the right argument name and all that.
EDIT:
Based on the answer below, I tried:
url(r'^%sdform/(P?<growl>.*)/' %(URLPREFIX), pv.dform, name='dform_message')
And changed the redirect to:
return redirect('dform_message', growl='Updated Settings')
I still get NoReverseMatch
I think your problem is that you shall not use the same names for different urls (django docu).

Django MongoDB Queryset can't print or iterate

I try to execute a MongoDB raw query in Django of the type:
queryset= ObjectClass.objects(__raw__={ })
if I want to print the queryset or iterate I get the following error message:
"error_message": "cannot convert value of type <class 'mongoengine.queryset.QuerySet'> to bson",
Any suggestions why this happens, I couldn't find a suitable answer so far, thanks for any hints
Jonas
just a shot into the dark since I m not able to try it out by myself in the moment, since I uninstalled mongodb. But in the back of my mind I remember that I had a similar problem.
Try:
queryset= list(ObjectClass.objects(__raw__={ }))
for result in queryset:
print result