Getting two database models to show on one html page - python

Currently working on a web page and I want to show two different tables of information one is for individual foods and the other is for recipes, however I can only get the first class to pull up any information I've tested to see if the first class can pull up the recipe database and it, in fact, currently out of ideas on what to try next.
class SearchFoodResultsView(ListView):
model = Food
template_name = 'nutrihacker/search.html'
context_object_name = 'food_list'
# overrides ListView get_queryset to find names containing search term and pass them to template
def get_queryset(self):
query = self.request.GET.get('term')
if (query == None):
return Food.objects.all()
else: # If there are any foods containing the query, they will be in the resulting object_list which is used by search.html in a for loop
food_list = Food.objects.filter(
Q(name__icontains = query)
)
return food_list
class SearchRecipeResultsView(ListView):
model = RecipePreset
template_name = 'nutrihacker/search.html'
context_object_name = 'recipe_list'
# overrides ListView get_queryset to find names containing search term and pass them to template
def get_queryset(self):
query = self.request.GET.get('term')
if (query == None):
return RecipePreset.objects.all()
else: # If there are any recipes containing the query, they will be in the resulting object_list which is used by search.html in a for loop
recipe_list = RecipePreset.objects.filter(
Q(name__icontains = query)
)
return recipe_list

Related

How to fetch data from another app Django

I'm creating a quiz project using Django with MongoEngine. Multiple Choice Questions and quiz are two separated apps. I want to fetch multiple choice in quiz based on m_id (unique number for each multiple choice). I'm a beginner and need a little help. How can i achieve thisMCQS Model
from django_mongoengine import Document, EmbeddedDocument, fields
class multichoice(Document):
m_id = fields.IntField(min_value=1, verbose_name='MCQ Id')
m_title = fields.StringField(primary_key=True, max_length=255, verbose_name='Title')
m_question = fields.StringField(verbose_name='Question')
m_alternatives = fields.ListField(fields.StringField(),verbose_name='Alternatives')
def __str__(self):
return self.m_title
Quiz Model
from django_mongoengine import Document, EmbeddedDocument, fields
from mcqs.models import *
class quiz(Document):
q_id = fields.IntField(min_value=1, verbose_name='Quiz ID')
q_title = fields.StringField(primary_key=True, max_length=255, verbose_name='Title')
q_s_description = fields.StringField(max_length=255, verbose_name='Description')
q_questions = fields.ListField(fields.IntField(), verbose_name='Question Numbers', blank=True)
def __str__(self):
return self.q_title
MCQ Views
def view_multichoice(request, m_id):
get_md = multichoice.objects(m_question_number = m_id)
return render(request, 'mcqs/mcq.html', {'get_md':get_md})
Quiz Views
def view_quiz(request, q_id):
get_q = quiz.objects(q_id = q_id)
return render(request, 'quiz/quiz.html', {'get_q':get_q})
Current Result
Expected Result
EDIT 1
Quiz Views
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
from mcqs.models import *
def view_quiz(request, q_id):
quiz_object = quiz.objects(q_id=q_id)[0]
multichoice_objects = [multichoice.objects(m_id=id) for id in quiz_object.q_questions]
get_q = [objects[0].m_question for objects in multichoice_objects if objects]
return render(request, 'quiz/quiz.html', {'get_q':get_q})
Quiz Template
{{ get_q }}
You are returning the question documents as it is. What you should be doing is, fetch the multichoice documents corresponding to the question ids, and then get the question field from each of those documents.
Change the second line in quiz views to this:
# Get the question document corresponding to given id
# objects method returns a list. Get the first one out of it.
# Here, I've assumed there's exactly 1 quiz document per ID. Handle the edge cases
quiz_object = quiz.objects(q_id = q_id)[0]
# Get the multiple choice documents corresponding to the ids in q_questions list
# Remember that objects method returns a list. Hence this is a list of lists
multichoice_objects = [multichoice.objects(m_id=id) for id in quiz_object.q_questions]
# Get the actual questions from the multichoice documents
get_q = [objects[0].m_question for objects in multichoice_objects if objects]
Ideally, you should be making multichoice as an EmbeddedDocument and the q_questions field in quiz model as EmbeddedDocumentListField. But the drawback here will be, you can't query EmbeddedDocuments independently. So you wont be able to do multichoice.objects(m_question_number=m_id).
You can read more about EmbeddedDocuments here

changeing ListView get_queryset? pagination in django

Good day everyone.
I am having some trouble with the paginator in django.
I have in my db all the customers info, so what I want to do is to display that info, but I made a search function in which you clic a letter button, for example you clic A and it will filter all the customers that their last name starts with the letter A, if you clic B , it will filter all customers with their last name start with B, and so on. This works correctly, the problem is that I also want to display 10 customers per page, so if I have 20 customers that their last name starts with the letter A, what you will see, will be 10 customers and a bar that says ( <<< page 1 page 2 >>> ) or something like that, and that should be solved with the paginator, I added it, but its not working.
I think the problem is that maybe my get function is rewriting the get_query function from ListView perhaps? I tryed different things but I'm not sure.
Here is my code in views:
class ExpedientView(ListView):
queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name')
template_name = 'dashboard-admin/portfoliorecords.html'
paginate_by = 10
def get(self,request):
if request.GET['letter'] == '':
context_object_name = 'portfolios'
queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name')
context = queryset
else:
letter = request.GET['letter']
context_object_name = 'portfolios'
queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name').filter(owner__last_name__istartswith=letter)
context = queryset
return render(request, 'dashboard-admin/portfoliorecords.html', {'portfolios': context})
the get(self,request) function, works perfectly, however the first part where the paginated_by is not working.
Before I added the get function, and just filtering all the customers, the paginator worked fine, so in the template, the code works properly.
You have to modify the def get_queryset(self) method not the def get(self, request) method
Remove the def get(self, request) method and the below code.
def get_queryset(self):
queryset = Portfolio.objects.filter(products__isnull=True).order_by('owner__last_name')
letter = request.GET.get('letter', None)
if letter:
queryset.filter(owner__last_name__istartswith=letter)
return queryset
If all you need to do is dynamically change the queryset then instead of overriding get (Which calls the appropriate functions in the view, which will perform all pagination tasks, etc.) you should be overriding get_queryset:
class ExpedientView(ListView):
queryset = Portfolio.objects.filter(products__isnull=True)
template_name = 'dashboard-admin/portfoliorecords.html'
paginate_by = 10
ordering = 'owner__last_name' # Put the ordering here instead of specifying it in the queryset
def get_queryset(self):
queryset = super().get_queryset()
letter = self.request.GET.get('letter')
if letter:
queryset = queryset.filter(owner__last_name__istartswith=letter)
return queryset

Django: Search results with django-tables2 and django-filter

I'd like to retrieve a model's objects via a search form but add another column for search score. I'm unsure how to achieve this using django-tables2 and django-filter.
In the future, I'd like the user to be able to use django-filter to help filter the search result. I can access the form variables from PeopleSearchListView but perhaps it's a better approach to integrate a django form for form handling?
My thought so far is to handle to the get request in get_queryset() and then modify the queryset before it's sent to PeopleTable, but adding another column to the queryset does not seem like a standard approach.
tables.py
class PeopleTable(tables.Table):
score = tables.Column()
class Meta:
model = People
template_name = 'app/bootstrap4.html'
exclude = ('id',)
sequence = ('score', '...')
views.py
class PeopleFilter(django_filters.FilterSet):
class Meta:
model = People
exclude = ('id',)
class PeopleSearchListView(SingleTableMixin, FilterView):
table_class = PeopleTable
model = People
template_name = 'app/people.html'
filterset_class = PeopleFilter
def get_queryset(self):
p = self.request.GET.get('check_this')
qs = People.objects.all()
####
# Run code to score users against "check_this".
# The scoring code I'm using is complex, so below is a simpler
# example.
# Modify queryset using output of scoring code?
####
for person in qs:
if person.first_name == 'Phil' and q == 'Hey!':
score = 1
else:
score = 0
return qs
urls.py
urlpatterns = [
...
path('search/', PeopleSearchListView.as_view(), name='search_test'),
... ]
models.py
class People(models.model):
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
Edit:
The scoring algorithm is a bit more complex than the above example. It requires a full pass over all of the rows in the People table to generate a score matrix, before finally comparing each scored row with the search query. It's not a one-off score. For example:
def get_queryset(self):
all = []
for person in qs:
all.append(person.name)
# Do something complex with all,
# e.g., measure cosine distance between every person,
# and finally compare to the get request
scores = measure_cosine(all, self.request.GET.get('check_this'))
# We now have the scores for each person.
So you can add extra columns when you initialise the table.
I've got a couple of tables which do this based on events in the system;
def __init__(self, *args, **kwargs):
"""
Override the init method in order to add dynamic columns as
we need to declare one column per existent event on the system.
"""
extra_columns = []
events = Event.objects.filter(
enabled=True,
).values(
'pk', 'title', 'city'
)
for event in events:
extra_columns.append((
event['city'],
MyColumn(event_pk=event['pk'])
))
if extra_columns:
kwargs.update({
'extra_columns': extra_columns
})
super().__init__(*args, **kwargs)
So you could add your score column similar to this when a score has been provided. Perhaps passing your scores into the table from the view so you can identify they're present and add the column, then use the data when rendering the column.
extra_columns doesn't appear to be in the tables2 docs, but you can find the code here; https://github.com/jieter/django-tables2/blob/master/django_tables2/tables.py#L251
When you define a new column for django-tables2 which is not included in table data or queryset, you should provide a render method to calculate it's value.
You don't have to override get_queryset if a complex filtering, preprocess or join required.
In your table class:
class PeopleTable(tables.Table):
score = tables.Column(accessor="first_name")
class Meta:
model = People
def render_score(self, record):
return 1 if record["first_name"] == "Phil" and q == "Hey!" else 0
In your view you can override and provide complex data as well as special filtering or aggregates with get_context_data:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["filter"] = self.filter
aggs = {
"score": Function("..."),
"other": Sum("..."),
}
_data = (
People.objects.filter(**params)
.values(*values)
.annotate(**aggs)
.order_by(*values)
.distinct()
)
df = pandas.DataFrame(_data)
df = df....
chart_data = df.to_json()
data = df.to_dict()...
self.table = PeopleTable(data)
context["table"] = self.table
context['chart_data']=chart_data
return context

Django/Taggit Searching for posts when all tags and title are contained in the database

I am trying to make a simple search system for my website. I am using python 3.6, Django 1.10.5 and Taggit 0.22.0. When I enter tags into the search form I want it to query the database and return any posts whose tags or title contain at least part of any tags searched.
For example: /search/?q=random%2C+stuff
would return:
Post1 {Title: "How to be funny" Tags: "Random", "isn't", "Funny"}
Post2 {Title: "How to make Stuffed Turkey" Tags: "Cooking", "Thanksgiving"}
models.py
class Post(models.Model):
title = models.CharField(max_length=256)
tags = TaggableManager()
def __str__(self):
return self.title
views.py
def index(request):
query = request.GET.get('q')
if (query == None or not query):
tags = Post.tags.most_common()[:10]
return render(request, 'search/search.html', {'tags':tags})
else:
queryList = query.split(',')
results = # Query the database to get results
return render(request, 'search/results.html', {'query':query, 'results':results})
I have been searching for the best way to achieve this query in django
Also the results should be case intensive, plugging TAGGIT_CASE_INSENSITIVE = True into settings.py has not worked for me.
Try with below code in your views:
from django.db.models import Q
q = request.GET['q'].split() # I am assuming space separator in URL like "random stuff"
query = Q()
for word in q:
query = query | Q(title__icontains=word) | Q(tags__name__icontains=word)
results = Post.objects.filter(query)
You may want something like this:
def products_search(request):
''' If I am intending to use this in other places also ,I should better put this in Model Manager'''
query_original=request.GET.get('q',None)
search_query = query_original.lower().split()
if len(search_query)>=1:
for word in search_query:
lookups = Q(title__icontains=word) | Q(description__icontains=word) Q(tags__name__icontains=word)
queryset = Product.objects.filter(lookups,is_active=True).distinct()
context = {'object_list':queryset}
return render(request,'products/product_search.html',context)

Feeding a current filter selection to another custom SimpleListFilter in Django

I am trying to make the prompts of one filter change in response to the current selection made in another filter. I am pretty lost as to how to get the currently selected value of the AttributeCategoryFilter passed into the AttributeFilter. I am using Django 1.4-dev. Trying to figure out if I should be using RelatedFieldListFilter for this purpose. It looks like these features are so young as to not have (m)any examples floating around in the wild yet.
class AttributeCategoryFilter(SimpleListFilter):
title = _('Attribute Category')
parameter_name = 'attribute_category'
def lookups(self, request, model_admin):
attributes = Attribute.objects.filter(parent_attribute=None)
prompts = []
for attribute in attributes:
prompts.append((attribute.title, _(str(attribute.title))))
return prompts
def queryset(self, request, queryset):
if self.value():
return queryset.filter(attribute__category=self.value())
else:
return queryset
class AttributeFilter(SimpleListFilter):
title = _('Attribute Title')
parameter_name = 'attribute_title'
def lookups(self, request, model_admin):
desired_category = # Needs to be a reference to the selected value in the AttributeCategoryFilter above
attributes = Attribute.objects.filter(category=desired_category).exclude(parent_attribute=None)
prompts = []
for attribute in attributes:
prompts.append((attribute.title, _(str(attribute.title))))
return prompts
def queryset(self, request, queryset):
if self.value():
return queryset.filter(attribute__title=self.value())
else:
return queryset
class ValueAdmin(admin.ModelAdmin):
list_display = ('package', 'attribute', 'presence', 'text', 'modified', 'created')
list_filter = ('package', AttributeCategoryFilter, AttributeFilter, 'presence',
'attribute__admin_approved', 'attribute__dtype', 'modified')
search_fields = ('package', 'attribute', 'text')
list_display_links = ('package', )
list_editable = ('presence', 'text')
list_per_page = 20000
admin.site.register(Value, ValueAdmin)
Here is what worked for me... The "TypeListFilter" is only visible once the "Category" filter is used and then displays all the entries that are a "subTypeOf" the selected category. The "special case" hack further down ensures that the filter disappears when the user selects another Category.
The "_class" parameter adds some extra flexibility. I am using the same filter with different but related Type classes and just have to override this one parameter. Just replace it by the admin.Model class that you want to filter.
class TypeListFilter( admin.SimpleListFilter):
"""
Provide filter for DnaComponentType (the actual "second level" type).
This filter has one cosmetic problem, which is that it's setting is not
automatically deleted if the category filter is changed. I tried but the
request and queryset are all immutable. Instead, the queryset method is
checking for any missmatch between category and filter name and filtering
is ignored if the category name doesn't match the current subType name.
"""
title = 'Type'
parameter_name = 'type'
_class = None
def lookups(self, request, model_admin):
"""
Returns a list of tuples. The first element in each
tuple is the coded value for the option that will
appear in the URL query. The second element is the
human-readable name for the option that will appear
in the right sidebar.
"""
if not u'category' in request.GET:
return ()
category_name = request.GET[u'category']
types = self._class.objects.filter(subTypeOf__name=category_name)
return ( (t.name, t.name) for t in types )
def queryset(self, request, queryset):
"""
Returns the filtered queryset based on the value
provided in the query string and retrievable via
`self.value()`.
"""
if not u'category' in request.GET:
return queryset
category = request.GET[u'category']
subtypes = self._class.objects.filter(subTypeOf__name=category)
r = queryset.filter(componentType__subTypeOf__name=category)
if not self.value():
return r
## special case: missmatch between subtype and category
## which happens after switching the category
if len(subtypes.filter(name=self.value())) == 0:
return r
return r.filter(componentType__name=self.value())
Found your question as I was looking for something else.
Here's what I did to only show players that had characters in a selected game:
def lookups(self, request, model_admin):
game_id = request.GET.get('game', None)
players = Player.objects.all()
if game_id:
players = players.filter(character__game_id=game_id)
return [(p.id, p.__unicode__()) for p in players]
Looks like this is what dan-klasson suggested too.
Hint: Putting ids into query parameters is generally considered a no-no for security reasons.

Categories

Resources