Query with multiple foreign keys (django) - django

I'm making a searchbar for a site I'm working on and I'm having trouble when I want to filter different fields from different models (related between them) Here are my models:
class Project(models.Model):
name = models.CharField(max_length=250)
objective = models.CharField(max_length=250)
description = models.TextField()
launching = models.CharField(max_length=100, null=True, blank=True)
image = models.ImageField(
upload_to='imgs/', null=True, blank=True)
image_thumbnail = models.ImageField(
upload_to='thumbnails/', null=True, blank=True)
slug = models.CharField(max_length=250)
class Meta:
db_table = 'project'
def __str__(self):
return self.name
class Institution(models.Model):
name = models.CharField(max_length=250)
project = models.ManyToManyField(Proyecto)
class Meta:
db_table = 'institution'
def __str__(self):
return self.name
And I want to be able to search by the name of the project or the institution, but my code only takes the institution's name.
def searchbar(request):
if request.method == 'GET':
search = request.GET.get('search')
post = Project.objects.all().filter(name__icontains=search, institution__name__icontains=search)
return render(request, 'searchbar.html', {'post': post, 'search': search})
How can I search for all the projects that match by its name OR the institution's name?
BTW, I'm using SQL, not sure if it's relevant, but I thought I should add that info.

You can .filter(…) [Django-doc] with Q objects [Django-doc]:
from django.db.models import Q
Project.objects.filter(Q(name__icontains=search) | Q(institution__name__icontains=search))
or you can work with the _connector parameter:
from django.db.models import Q
Project.objects.filter(
name__icontains=search,
institution__name__icontains=search,
_connector=Q.OR
)

Related

Django Rest Framework how to serialize a many to many relational Model?

I am doing a project in Django Rest Framework. Now I am trying to serialize many to many relations, but I don't know how this works. Here is my models' code:
Model for files
def user_directory_path(instance, filename):
return 'user_{0}/{1}'.format(instance.user.id, filename)
class Document(models.Model):
name = models.CharField(max_length=250, blank=True, null=True)
document = models.FileField(upload_to=user_directory_path)
def __str__(self):
return self.name
Model for Expenses and loans
class Expenses(models.Model):
name = models.CharField(max_length=250, blank=True, null=True)
amount = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
date = models.DateField(auto_now=True)
additional_files = models.ManyToManyField(Document, blank=True, related_name="expenses") # Upload multiple files
def __str__(self):
return self.name
class Loans(models.Model):
name = models.CharField(max_length=250, blank=True, null=True)
amount = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
loan_from = models.CharField(max_length=250, blank=True, null=True)
date = models.DateField(auto_now=True)
additional_files = models.ManyToManyField(Document, blank=True, related_name="loans") # Upload multiple files
def __str__(self):
return self.name
My question:
Just want to know how to serialize these additional_files in the Expenses and Loans.
It will be much better if give resources and explanations of how that works.
For List endpoint you could make something like that:
serializers.py
class ListDocumentSerializer(serializers.Serializer):
# Document fields
class ListLoansSerializer(serializers.Serializer):
name = serializers.CharField()
additional_files = ListDocumentSerializer(many=True)
# other loans model fields
views.py
class ListLoansApi(APIView):
permission_classes = []
def get(self, request):
loans = Loans.objects.prefetch_related('additional_files') # prefetch_related for ORM optimization with M2M
loans_data = ListDoctorSerializer(loans, many=True).data
return Response(loans_data)

How to change the queryset returned based on the URL

I am trying to make an educational site and have made a category system. There is a URL for each category and I need change the queryset returned based on the URL I am on. For example if I am on "localhost:8000/posts/category/3", I want my queryset returned to be:
Post.objects.filter(category=3).order_by('-date_posted')
And so one depending on the URL.
I don't quite know where to start from for this.
The class based view that returns the queryset:
class CatPostListView(ListView):
model = Post
template_name = 'blog/science.html' #This is when you click a profile in a post, it takes you to his posts only
context_object_name = 'posts'
paginate_by = 15
def get_queryset(self):
return Post.objects.filter(category=2).order_by('-date_posted')
urls.py (Contains only the part necessary):
urlpatterns = [
path('post/category/<int:pk>/', CatPostListView.as_view(), name='category')
]
And just in case models.py:
class Category(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField()
parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.SET_NULL)
class Meta:
# enforcing that there can not be two categories under a parent with same slug
# __str__ method elaborated later in post. use __unicode__ in place of
# __str__ if you are using python 2
unique_together = ('slug', 'parent',)
verbose_name_plural = "categories"
def __str__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
category = models.ForeignKey('Category', null=True, blank=True, on_delete=models.SET_NULL)
display = models.TextField(max_length=250)
date_posted = models.DateTimeField(default=timezone.now)#DON'T USE () HERE Just auto_now_ will show the date of the current day
author = models.ForeignKey(User, on_delete=models.CASCADE)#No () #This deletes the posts when the user is deleted
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
If I use the view i posted here that will not quite work. I need a method to take the pk from the url and put it in views.
You can get the value from the URL:
return Post.objects.filter(category=self.kwargs['pk']).order_by('-date_posted')

join two tables in django admin to get columns for display_list

I am having problem with list_display/joining two table for django admin interface.
I want to have columns- program_name and is_active from SchPrograms model with parameter_name, parameter_description from VisVisitParameters model in django admin. I am able to have those columns which i am using with return in each of these models. I tried to take help from the question that already has been asked. But still i could not able to figured this out.
class SchPrograms(models.Model):
program_id = models.AutoField(primary_key=True)
program_name = models.CharField(max_length=200, blank=True, null=True)
creation_timestamp = models.DateTimeField(blank=True, null=True)
is_active = models.IntegerField()
class Meta:
managed = True
db_table = 'sch_programs'
app_label = 'polls'
def __str__(self):
return self.program_name
class VisVisitParameters(models.Model):
vparameter_id = models.AutoField(primary_key=True)
parameter_name = models.CharField(max_length=300, blank=True, null=True)
parameter_description = models.TextField(blank=True, null=True)
is_active = models.IntegerField(choices=STATUS_CHOICES)
class Meta:
managed = False
db_table = 'vis_visit_parameters'
def __str__(self):
return str(self.vparameter_id)
app_label = 'polls'
class VisVisitParameterMappings(models.Model):
vp_map_id = models.AutoField(primary_key=True)
vparameter = models.ForeignKey(VisVisitParameters,on_delete=models.CASCADE)
program = models.ForeignKey(SchPrograms,on_delete=models.CASCADE)
display_order = models.IntegerField(blank=True, null=True)
is_active = models.IntegerField()
class Meta:
managed = False
db_table = 'vis_visit_parameter_mappings'
app_label = 'polls'
def __str__(self):
return str(self.parameter_name)
model.py
class VisVisitParameterMappings_admin(admin.ModelAdmin):
list_display = ('program_name','is_active','parameter_name','parameter_description ')
To display the required items on the list display page you can write your custom methods, as documented here.
For example, for your field named program_name you can have:
def program_name(self, obj):
if obj.program:
return obj.program.program_name
program_name.short_description = 'Program name'
and for another model field named parameter_name , you may have:
def parameter_name(self, obj):
if obj.vparameter:
return obj.vparameter.parameter_name
parameter_name.short_description = 'Parameter Name'
Hope it helps.

Django user model, backward look up is not working

I've made a foreign key relationship with django User model, the forward lookup is working fine but when I try to backward is throwing this error:
'QuerySet' object has no attribute 'urlpost_set'
I have also tried the related name! Also note that the Catagory to PostUrl and PostUrl to Catagory is working just fine!
My models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Catagory(models.Model):
title = models.CharField(max_length=15, unique=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
def __str__(self):
return self.title
class Meta:
verbose_name_plural = 'catagory'
class UrlPost(models.Model):
STATUS_CHOICES = (
('public', 'Public'),
('private', 'Private'),
)
profile = models.ForeignKey(User, related_name='user_post', on_delete=models.CASCADE)
catagory = models.ForeignKey(Catagory, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
slug = models.SlugField(unique=True)
url = models.URLField()
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='public')
note = models.TextField(blank=True)
created = models.DateField(auto_now_add=True)
updated = models.DateField(auto_now=True)
class Meta:
ordering = ['-created']
verbose_name_plural = 'url Post'
def __str__(self):
return self.title
You have set related_name='user_post' while defining ForeignKey relation between your User model and UrlPost.
You have to use .user_post.all() instead of .urlpost_set.all() in your queryset.

Django query based on ManyToManyField

I am new to Python and Django.
I am trying to build myself very simple blog application.
So I have this 2 models :
class Tag(models.Model):
name = models.CharField(max_length=250)
slug = models.SlugField(unique=True)
def __unicode__(self):
return self.name
class Blogpost(models.Model):
title = models.CharField(max_length=300)
content = tinymce_models.HTMLField()
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
tags = models.ManyToManyField(Tag)
def __unicode__(self):
return self.title
As you can see Blogpost can contain many Tags,
my question is how can I query Blogpost.objects.all() to get Blogposts list by specific Tag?
Thank you.
I think related manager is your answer
t = Tag.objects.get(name="Some tag")
t.blogpost_set.all()