How to query tables having manyTomany field in django - python

class Publication(models.Model):
title = models.CharField(max_length=30)
pub_date = models.DateField()
class Article(models.Model):
headline = models.CharField(max_length=100)
publications = models.ManyToManyField(Publication)
I actually need a single query that can get all "title" and "headline".
I need another query for where i can filter using either title or headline.
Lastly, I want to filter using both column e.g select all where title="Money" and publication="money talks"
I used this code but its giving me error.
Publication.objects.filter(pub_date__year=2016).select_related(publications)
Thank you

Related

django modeltranslation doesn't work with such queryset

I'm new to programming.
In my blog I want to show a list of categories.
If I create a queryset like this:
Category.objects.all()
my django-modeltranslation works perfectly.
But I want to get categories of only published posts. Then my queryset is:
Post.objects.values('category__name').filter(is_published=True)
However, django-modeltranslation doesn't work. I get values from 'name' field instead 'name_en' or 'name_ru' fields.
What is wrong?
Here's my models.py :
class Category(models.Model):
name = models.TextField(max_length=100)
url = models.SlugField(max_length=160, unique=True)
class Post(models.Model):
title = models.TextField('title', max_length=150)
category = models.ManyToManyField(Category, related_name='posts', blank=True)
I think you better query in reverse: with .values(…) you select a specific database column, so this will omit the model logic.
You can retrieve the categories with:
Category.objects.filter(posts__is_published=True).distinct()

django distinct in template by filter

I need to display unique values in template. I know distinct will work in query. But table in one row Many to Many field I don't know how to implement for that. If any possibilities are there to use it in template
class DoctorFeedback(models.Model):
visited_for = models.CharField(max_length=200)
tag = models.ManyToManyField(Tags)
class Tags(models.Model):
tag_title = models.CharField(max_length=50)
You can add unique=True to Tags, so there will never be any duplicates:
class Tags(models.Model):
tag_title = models.CharField(max_length=50, unique=True)

Django model query all fields including the ManyToManyField in a single command

While playing around with the ManyToManyField, I wondered is there a way you can query a ManyToManyField automatically the same way you'd do a ForeignKey using select_related()?
Tables:
class Author(models.Model):
fullname = models.CharField(max_length=100)
class Foo(models.Model):
bar = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
foo = models.ForeignKey(Foo)
author = models.ManyToManyField(Author)
In order to get the data I need from Book I usually do:
book = Book.objects.select_related('foo').get(pk=1)
authors = book.author.all()
which makes 2 trips. Is there a way to combine them the way select_related() does?
You can use prefetch_related for M2M field
Book.objects.select_related('foo').prefetch_related('author').values('author', 'title').get(pk=1)

Query for only part of objects of related_name (from ForeignKey)

I have to models:
class Blog(models.Model):
name = models.CharField(max_length=100)
tagline = models.TextField()
class Entry(models.Model):
blog = models.ForeignKey(Blog,related_name='entries')
headline = models.CharField(max_length=255)
body_text = models.TextField()
I would like to get all Blogs and inside each blog I would have that all entries where headline is 'bike'.
So the output will be list of all Blogs and the blog.entries will have the filtered list of entries.
You can use Prefetch:
entries = Entry.objects.filter(headline='bike')
blogs = Blog.objects.all().prefetch_related(Prefetch('entries', queryset=entries))
You can do this query
Blog.objects.filter(entries__headline='bike')
It retrieves all the blogs that have an entry with the headline as bike ina single query. So it's a lot easier on the server see Spanning Multi Value Relationships

Django filter objects with at least one many-to-many having attribute of value

I'm looking to do a complex filter using Django's ORM.
Models:
class Book(models.Model):
title = models.TextField()
bestseller = models.BooleanField(default=False)
class Author(models.Model):
name = models.TextField()
books = models.ManytoManyField(Book)
How would I query for all authors who have at least one best-selling book?
Query:
best_authors = Author.objects.filter(<relevant filter>)
Edit:
According to the documentation, the following should work:
best_authors = Author.objects.filter(books__bestseller=True)
Unfortunately, that ends up returning repeated author objects (the same author for each bestselling book of his/hers, over and over).
best_authors = Author.objects.filter(books__bestseller=True).distinct()
The filter() does a JOIN with the Books table and produces all rows where bestseller==True. The distinct() ensures that each author is listed only once in the results.

Categories

Resources