Performance of many to one relation in django - django

I have a website which features famous people (each person is a row in the table People). For each famous person I would like to list related websites. I thought about creating a table called Websites and define it in the models.py module as follows:
class Websites(models.Model):
website_url = models.CharField(max_length=200)
related_person = models.ForeignKey(Person)
Then when I load a person's page I will run a wesbsites_set() query to fetch all related website.
However, won't that kind of query "cost" in load time? Isn't there a better solution to design this problem so I won't have to run a query against the Websites table on each person's page load? The table itself will contain many rows!
Thanks,
Meir

that's the SQL way to do it. The foreygn key will create a key to the People's table and as such the query will rely on the table indexes to be faster. Go for it :)

Related

Availability Schedule field in Django Models?

I am quite new to Django and I'm trying to build a web application where some users will be able to input their available weekly schedule and also show at what times of a day they are available. The picture below(this is just front-end part) shows something similar to what I'm trying to build. What type of Model field do I use to store this type of information inside of a Model class?
I would use a ManyToMany field which links to a Shift table.
class Shift(models.Model):
# Day of week
day = models.CharField()
# Morning, afternoon, evening
time = models.CharField()
class UserProfile(models.Model):
...
availability = models.ManyToManyField(Shift)
The Django docs have a handy guide on using ManyToManyFields.
https://docs.djangoproject.com/en/4.1/topics/db/examples/many_to_many/
Now that I look at it I can try to create many MultiSelect fields for each of the days(Sunday,Monday,Tuesday,...Saturday) and each of them will have 3 choices(Morning, Afternoon, Evening). This way you wouldn't need to prepopulate the database(before using the database) with all different options(Sunday Morning,Monday Evening etc.) However, I might run into the trouble of having redundancy in my table in the long run.

Conditional Relationships between Two Tables in Django?

The following image shows a rough draft of my proposed database structure that I will develop for Django. Briefly, I have a list of ocean Buoys which have children tables of their forecast conditions and observed conditions. I'd like Users to be able to make a log of their surf sessions (surfLogs table) in which they input their location, time of surf session, and their own rating.
I'd like the program to then look in the buoysConditions table for the buoy nearest the user's logged location and time and append to the surfLog table the relevant buoyConditions. This will allow the user to keep track of what conditions work best for them (and also eventually create notifications for the user automatically).
I don't know what the name for this process of joining the tables is, so I'm having some trouble finding documentation on it. I think in SQL it's termed a join or update. How is this accomplished with Django?
Thanks!

Django create a view to generate a receipt

I want to create a small app that creates a kind off receipt record in to a db table, from two other tables. very much like a receipt from a grocery store where a cashier makes a sell and the ticket contains multiple items, calculates a total and subtotal and return values to the database. I currently have 3 tables: the Ticket table where i would like to insert the values of all calculations and ticket info, the services table that acts like an inventory of services available. this has the service name and price for each service and my responsible table that has a list of "cashiers" or people that will make the sale and their percentage for their commissions, i have the views to create , edit and delete cashier's and services.
What I don't have is a way to create the ticket. I am completely lost. can you guys point me in to the correct path on what to look for. i am learning to program son i don't have a lot of knowledge in this if its even possible. i don't need the system to print i just want to have all record stored this way later on i can expand on it and create reports of sold items and who sold them and how much commissions each seller has won.
You need to create relationships to two other models (tables) from the Ticket model (table). Luckily you don't have to create the relations in the database tables itself. Use django model's Foreign key fields to accomplish this. Here is the documentation link:
Django Models
You may need to read it several times to get the concepts thoroughly.

How to know where database has changed

I have a project that looks like a simple shopping site that sells different kinds of products. For example, I have 4 models: Brand, Product, Consignment. Consignment is linked to Product, and Product is linked to Brand. To reduce count of queries to databases, I want to save current state of these models(or at least some of them). I want to do it, because I show a sidebar with brands and products. So every time when user opens some page, it will execute the query to database to get those brands and products.
But when admin add some new product or brand, I want to handle database changing and resave it. How to implement it?
Your answer is by using Cache. Cache is a method to store your objects in memory/other app like redis temporarily so that you do not need send queries to database. You can read the full description here.
Or, you can use this third party library that helps you to cache Django ORM Model. Here are the example.
Brand.objects.filter(name='stackoverlow').cache()
After doing an update to the model, you need to clear or invalidate the cache.
invalidate_model(Brand)

How to create django models Dynamically

My django application need to collect user data(name age country etc) based on his email domain( 'gmail' as in xyz#gmail.com).I wist to create a new table every time i encounter a new email domain.
Can this be done in django ?
This is a bad idea. Your tables would all have the same structure. All of your data should be stored in a single table, with a domain column to keep the data separate. Why would you want a different table for each domain? Whatever reason you have, there's a better way to do it.
This idea goes against everything in the design of the relational database, and the Django ORM on top of it.