Django - SubQuery e function F() - python

I have 2 models (author and book). I want to annotate in the author model the highest book price for every authors (in order to manipulate it later). I would like to make a query with an internal sub-query using the F() function but the problem is that the F() is refered to the book model but I would like to refer it to the author model. I'll show that with an example. The models are shown below:
class author:
authorID = models.AutoField(primary_key = True, auto_created = True, unique = True)
name = models.CharField(max_length=100)
surname = models.CharField(max_length=100)
class book:
ownerID = models.ForeignKey(author, on_delete = models.CASCADE, related_name='hook')
title = models.CharField(max_length=100)
price = models.IntegerField()
The query in question is as follows:
value = author.objects.annotate(maxPrice = (book.objects.filter(ownerID = F('authorID')).order_by('-price') [0]))
F('authorID') is refered to the book DB and so Django returns an error but I want to refer it to the author DB to achieve my goal. How can I do that? Thanks

Related

How can I access a specific field of a queryset in Django?

I have these models in my Django app:
class Book(models.Model):
title = models.CharField(max_length=50, unique=True)
owner = models.CharField(max_length=30)
price = models.DecimalField(max_digits=5, decimal_places=2, null=True)
book_rating = models.ForeignKey('Rating', null=True)
RATE_CHOICES = zip(range(1,6), range(1,6))
class Rating(models.Model):
user = models.ForeignKey(User)
this_book = models.ForeignKey(Book)
rate = models.DecimalField(max_digits=2, decimal_places=1, choices=RATE_CHOICES)
comment = models.TextField(max_length=4000, null=True)
I am trying to access the Ratings of each instance of the Book model. Here is what I've tried so far in the shell:
from django.contrib.contenttypes.models import ContentType
>>> ctype = ContentType.objects.get_for_model(Rating)
>>> ctype
<ContentType: rating>
>>> book_titles = ctype.model_class().objects.filter(this_book__title='My Test Book')
>>> book_titles
<QuerySet [<Rating: My Test Book - parrot987 - 3.0>, <Rating: My Test Book - 123#gmail.com - 5.0>]>
How can I access the two rating values of each object (5.0 and 3.0) without all of this other data?
Can this be done in such a way that I am able to average the numbers and return the final value?
For 1. you can use (relevant documentation):
Rating.objects.filter(this_book__title='My Test Book').values('rate')
If you just want a flat list you can use values_list('rate', flat=True) instead of values('rate').
For 2 (relevant documentation):
from django.db.models import Avg
Rating.objects.filter(this_book__title='My Test Book').aggregate(Avg('rate'))
This will return a dictionary where the key is rate__avg and the value is the average of the ratings.
Please see the following for Many to One fields django - Get the set of objects from Many To One relationship
To access the rating, you can use a for loop and access the individual values e.g.
total = 0
for rating in book_titles.book_set.all()
total += rating.rate
Good luck!

Django getting information from three related tables. Joining tables

I have three tables that are related.
class Book(models.Model):
year_published = models.IntField()
author = models.ForeignKey(Author)
class Author(models.Model):
author_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
agent = models.ForeignKey(LitAgent)
class LitAgent(models.Model):
agent_id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50)
Ok, I can get a LitAgent like so
getla = LitAgent.objects.get(agent_id=1)
I can get the authors like so
getauthors = Author.objects.filter(agent=getla.agent_id)
But how can I get all the books that an author has too and make sure the books line up to the right author? I also need access to the data in LitAgent and Author too
From my understanding, you want to get the books by using an agent_id. If this is what you want then you can accomplish this using
books = Book.objects.filter(author__agent__agent_id=1)
This will return a list of all books that the author related to the agent with id = 1. If you want to access the author for each book you can use
for book in books:
print book.author.name
# in order to print the author agent
print book.author.agent.name
On the other side you can reach the books from the LitAgent model.
Lets say
agent = LitAgent.objects.get(agent_id=1)
Then to get the authors it will be
authors = agent.author_set.all()
Now you can iterate
for author in authors:
for book in author.book_set.all()
print book

How to connect data between author and book table which has ManyToMany relation in Django

I am learning Django and have been complete Model's chapter by "The Django Book" but didn't get the way to insert value in ManyToMany and ForeignKey for below question:
In this below model, How do i insert detail of "Author" and "Publisher" for book name = "The book" with publication-date = "28/10/2013"
And after inserting value, how do i get back "Author" and "Publisher" for book name = "The book"
class Author(models.Model):
first_name = models.CharField(max_length = 20)
last_name = models.CharField(max_length = 20, blank = True)
email = models.EmailField(blank = True)
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
class Book(models.Model):
title = models.CharField(max_length=100)
publisher = models.ForeignKey(Publisher)
authors = models.ManyToManyField(Author)
publication_date = models.DateField()
if book is already there in database.
just simply run like
book_obj = Book.objects.get(title="Test Title")
author1 = Author.objects.get(name="test author1")
author2 = Author.objects.get(name="test author2")
author3 = Author.objects.get(name="test author3")
book_obj.authors.add(author1, author2, author3)
Same you can add Publisher if book is already in database.
If you want to create new entry of book.
publisher = Publisher.objects.get(name='Test publisher')
author = Author.objects.get(name='Test author')
Book.objects.create(title='Title1', publication_date='28/10/2013', authors=author, publisher=publisher)
Let's say you want to add a Stephen King's book Pet Semetary. You need to have the publisher and author instances already before adding the book object. Then it's very simple:
publisher = Publisher.objects.get(name='Simon & Schuster')
author = Author.objects.get(name='Stephen King')
new_book = Book(title='Pet Semetary',
publication_date='28/10/2013',
authors=author,
publisher=publisher)
new_book.save()
Is this the solution you were looking for?
update on comment
If the book object exists you can just apply it like so:
# create Author and Publisher:
new_author = Author(first_name='Stephen', last_name='King')
new_author.save()
new_publisher = Publisher(...bla bla...)
new_publisher.save()
# link them to your already existing book instance
book_instance = Book.objects.get(title='Pet Semetary')
book_instance.author = new_author
book_instance.publisher = new_publisher
Of course, you should use less general names than new_author or book_instance but you get the idea

Copy or clone an object instance in Django/Python

I've following scenario:
class CourseTemplate(models.Model):
title = models.CharField(max_length=70)
teacher = models.ForeignKey(User)
description = models.TextField()
max_students = models.IntegerField()
sessions = models.ManyToManyField(CourseSession) # e.g. Session 1 Introduction, Session 2 Basics, etc.
rating = models.ManyToManyFields(StudentRating)
date_added = models.DateTimeField()
class CourseEnrollment(models.Model):
course = models.OneToOneField(CourseTemplate) # Each enrollment needs a new CourseTemplate Instance, so I can track it
students = models.ManyToManyField(User)
Class CourseSession(models.Model):
title = models.CharField(max_length=50)
date = models.DateTimeField()
details = models.CharField(max_length=100)
address = models.TextField()
#parent_course = models.ForeignKey(CourseTemplate)
class StudentRating(models.Model):
student = models.ForeignKey(User)
rating = models.IntegerField()
#course = models.ForeignKey(CourseTemplate)
Now a teacher (=User) can create a CourseTemplate with all the required details first. After it's saved, he can create a concrete "enrollment" for e.g. this semester with 5 sessions. Maybe he changes after 8 enrollments some details (e.g. CourseTemplate.description or the course now only has 7 sessions instead of 8).
I'd like to have a 1:1 relationship between each CourseTemplate instance and each CourseEnrollment, so I can see for example:
- Teacher X had 2012 three CourseEnrollments, two of them were the same or
- which rating has he received for his second course.
The presented "Template" should always be the "newest", so I'd just need to get the latest instance by CourseTemplate.date_added.
Does anyone know how I can avoid this problem?
Thanks a lot!
You can duplicate any existing django model instance by clearing its primary key, and then saving it again.
ct = CourseTemplate.objects.all()[0]
print ct.pk
# some original pk
ct.pk = None
ct.save()
print ct.pk
# will be a new auto-incremented

Django - storing logical tests as records

I'm working on a Gran Turismo 5 Django application. Here's a very simplified data model:
class Event(models.Model):
name = models.CharField(max_length=256, unique=True)
class EventCriteria(models.Model):
name = models.CharField(max_length=256, unique=True)
events = models.ManyToManyField(Event)
test = ???
class Country(models.Model):
name = models.CharField(max_length=256, unique=True)
class Make(models.Model):
name = models.CharField(max_length=256, unique=True)
country = models.ForeignKey(Country)
class Car(models.Model):
name = models.CharField(max_length=256, unique=True)
make = models.ForeignKey(Make)
class Setup(models.Model):
name = models.CharField(max_length=256, unique=True)
car = models.ForeignKey(Car)
horsepower = models.IntegerField()
For example, a given event might have the criteria 'Country = Italy'. When applied against the model above, that would require a test like the following:
setup.car.make.country.name == u'Italy'
Does anyone have a good framework for how I might structure the EventCriteria model (especially the 'test' field or fields') to make a) storing these tests and b) applying them as filters in future views possible?
Thanks,
Mike
It's not clear on why your "test" isn't a simple boolean field. The question is confusing.
I'm assuming that really want a persistent filter, since that's often requested.
A Django filter is a dictionary.
SomeModel.objects.filter( column=value, column__contains=value )
SomeModel.objects.filter( **{'column':value, 'column__contains':value} )
You can do this to persist your "test".
Convert your "filter" expression to a dictionary.
JSON-encode the dictionary as a BLOB
Save it.
You can apply your test as follows.
Get the filter BLOB
JSON-decode the dictionary
Use the dictionary in a filter for the appropriate class.

Categories