Availability Schedule field in Django Models? - django

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.

Related

What is the best way to build Django model?

I want to make a website that gives a visualization of football game statistics.
Functionality: The user checks a list of games. Selects game to see details. Can select a particular player. If there is no game he/she is interested in, the user uploads a datafile of the game and adds it to the main list of games.
I have a script that cleans data and gives me DataFrame with columns:
['Shots', 'SCA', 'Touches', 'Pass', 'Carries', 'Press', 'Tackled', 'Interceptions', 'Blocks']
if I do the Django model is it ok if I simply do model with these columns, or do I need to do a proper database with separate tables like:
Is my UML is ok or do I need to fix something?
here is link to my UML https://drawsql.app/xml-team/diagrams/football-game-stats
You don't need to manually create tables and relations in your database, Django can take care of that for you.
It has been explained in detail here
I suggest following these simple steps for building Django Model. I always did by following these steps.
Create Django Application
Add the Posts Model
Update Settings
Make Migrations
Verify Database Schema

When does django query the database when acceessing a related model instance through the foreignkey relationship?

This is an example model definition:
class Glass(model):
type = models.CharField(max_length=255)
class Window(model):
glass = models.ForeignKey(Glass)
class House(model)
window = models.ForeignKey(Window)
Dotwalking the foreign keys of the mentioned models and getting the related objects.
house = House.objects.get(pk=1)
window = house.window
glass= house.window.glass
I understand that to get house a query is run. Then when I get house.window I presume a second query is run to get an instance of the window. And when accessing glass does Django already have the window loaded in the instance of House? Or does it query window again?
I can't seem to find the exact answer to this question online or in the Django docs.
Answer would be great to further my understanding of Django ORM querying.
Cheers!
each time you trigger the orm a query is made, the first line creates an instance of house and to do that it loads it with data, the same is true for the second line but for an instance of window and for the third line for an instance of glass. You can merge those queries by adding prefetch_related as a paramter in the first query. If you do not want the second and/or third query you can access the id field directly by using house.window_id(bypass the orm by accessing the attribute directly). If you do use house.window.id however you will trigger the orm to create a window instance.

how to populate static database tables in django

I am trying to learn Django and so far I have been able to create models and be able to inject data into the database using views and have a small web app working nicely.
I have a small confusion regarding how Django works with tables that will need to be populated outside the application.
For example, I should have a table called Products. Now the products change extremely infrequently and moreover there are some products that the database should already be populated with. For example, my product model could be simple as:
class ProductModel(models.Model):
name = models.CharField(max_length=200)
What is the Django-nic way to pre-populate this table with a set of products already?
For Products that should already be populated you could use initial data to prepopulate
If some external software wants to change the product list, well hmm if you need to do it manually you can create a data migration
...or you could create a view that takes a json as input ant populates the Projects table using bulk_create() or a simple create()
...or you can create a custom command that another program can call like python manage.py import_products my_products.json
...or make the external application to insert the products into the same database will be ok too.

No CRUD operations with Django/Tastypie

I am a newbie with Tastypie and it is wonderful the way you can achieve CRUD operations with it so quickly. But I would like to implement other kind of web services where the return value is other than a model. For example, if I had a simple model like this
class User(models.Model):
name = models.CharField(max_length=20)
age = models.PositiveSmallIntegerField()
and wanted to get the average age of all users via /api/v1/user/avg_age, how should I do it? Perhaps it is something related to Django URLs more than Tastypie but I am lost at this moment. So, the question is where/how should I define my custom REST web services?
Thanks in advance
You can add the method to the model itself or put it in a service layer. After doing so you can easily add the value to the resource with a dehydration cycle.
Another option, which will allow filtering on the value, is to implement a model holding this data, e.g. a UserStatistics model. You can then add a foreign key relationship or create a stand-alone resource.
Because data won't likely change a whole lot and these calculations are more expensive I would encourage you to create a cronjob or task for such a model, only executing database writes periodically

Performance of many to one relation in 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 :)