I have a function that connect database and gets sales from sql server for every book.
import website.queries as query
def book_list(request, user_id):
user = User.objects.get(id=user_id)
books = user.book.all()[::-1]
sales = {}
for book in books:
if book.is_active:
sales[book.isbn] = query.get_total_sales(book.isbn)
return render(
request,
'book-list.html', {
'user': user,
'books': books,
'sales': sales
}
)
and query
def get_total_sales(code):
with connections['logo'].cursor() as cursor:
cursor.execute(
f"""
SELECT * FROM URUNLER_TOPLAM WHERE CODE = '{code}'
"""
)
column_names = [column[0] for column in cursor.description]
query_result = [
dict(zip(column_names, row)) for row in cursor.fetchall()
][0]
total_sales = 0
for column in query_result:
if 'TOPLAM' in column:
total_sales += int(query_result[column])
return total_sales
Sometimes book_list function response, took too long
Also this is how I connect to the database in functions, is this a correct method?
Books are in no particular order, so i just reversed the order of addition.
I make this application only to show the sales amount of the books, i get the sales volumes from a remote server
How can I create a suitable model for this?
Please ignore my stupidity
Btw, books are stored in a database on the server, along with their trivial information.
Important information is on the remote server that I connect to
Books are linked to specific users and retrieve important information from the remote server when the user logs in
It’s a lot better to use the order_by query clause. You can order_by the id field if you want to sort it by insertion order. See order_by and ordering.
Are you saying that the table schema is being modified constantly?
You’re not. A Django model is a Python representation of a table. Creating such an object has no effect on the database.
That’s precisely a reason to do it.
I’ve posted a number of times here describing where I’ve got projects that retrieve data from legacy databases owned by other projects. It’s a very routine operation.
Everything from online, wholesale or retail book sales is done through the program on the remote server. I get the information of these sales in my application.
Yep. So that’s an excellent reason to create a model for those tables. Using the ORM for your queries will help facilitate that - far more than generating raw SQL and post-processing resultsets yourself.
Start with the documentation links I provided earlier.
Side note: It may be a language or translation issue, but your phrasing:
possibly indicates an incomplete understanding of what the ORM is and what Models do.
A Model is a Python object - that’s all. It’s a container for a set of attributes that map to the columns of a database table. (It also allows for the definition of some related behavior that may be created or assigned to it)
The ORM issues queries to a database, gets the responses, and then creates instances of the Model based upon the data returned from the database by the query. Each row returned creates a new instance of the model. The data for each column of the result set is assigned to the corresponding field for that instance of the model.
If you use it as a “query-only” (no updates) object, that’s all it does. There’s no “magic” here. There’s nothing going on that is going to make this appear to the database as anything different from any other query being issued.
As an example, you have the query:
This is going to cause the ORM to execute something like this SQL statement: select * from auth_user where id = user_id
It’s then going to create an instance of the User object named user, and use the output of that query to populate the fields in user. That is really all that is happening here.
Then remove the fields you don’t need. Don’t inspect models you don’t need.
You can create models for views.
I’m sorry, but I’m not understanding your reluctance to use the “Django way” of handling this. If you’re not going to use Django’s facilities, then why are you using Django?
The very first note of that page that you’re referencing:
Ok, so going back to your original question, you were looking for a way to improve the performance of your queries.
I have given you a direction to take to do so. Whether you choose to use this information is up to you. But if you choose not to use it, I’m not sure how I can help you.