Django models, 2 foreign keys, object logic - python

(Capitalized the objects within the process).
Is this considered legal, my goal is to have 3 individual objects (the
reasoning behind this is to allow me to assign a specific school +
major to specific professors). When a user is to select a SCHOOL, I
want my website to pull the MAJORS that specifically correspond to
that SCHOOL, and when they select a MAJOR, I want the specific
PROFESSORS to display.
Logical connection between the objects below (business rules)
The SCHOOL needs to be a many-to-many relationship with MAJOR:
One SCHOOLS can have MANY Majors
One Major can have MANY SCHOOLS
The PROFESSOR
a PROFESSOR can work at ONE SCHOOL, and teach ONE MAJOR
a Major can be taught by MANY PROFESSORS, at MANY SCHOOLS
So my difficulty is writing out the logic of the models to ensure all conditions are met, the information set before the rules is the experience I want my users to have when it comes to information pulled from the database to the website, and the logic below it.
from django.db import models
class Professor(models.Model):
School = models.ForeignKey(School , on_delete=models.CASCADE)
Major = models.ForeignKey(Major , on_delete=models.CASCADE)
class Major(models.Model):
Major = models.CharField(max_length=30)
School = models.ForeignKey(School , on_delete=models.CASCADE)
class School(models.Model):
School = models.Charfield(max_length=50)
Also, if you have any recommendations on making this logic more clear, I would grately appreciate it!

Try using many-to-many fields for the relationship between schools and majors. I would add it onto the school since I personally think about it as "Schools have many majors" more frequently than "Majors are taught at many schools" but both would be fine.
Also, set the text fields to be something like name since that's what the text field represents: not the school/major, but rather the name of the school/major. Other than that, your logic looks good
from django.db import models
class Major(models.Model):
name = models.CharField(max_length=30, db_index=True)
class School(models.Model):
name = models.Charfield(max_length=50, db_index=True)
majors = models.ManyToManyField(Major)
class Professor(models.Model):
school = models.ForeignKey(School , on_delete=models.CASCADE)
major = models.ForeignKey(Major , on_delete=models.CASCADE)
Once you've got this, you can grab major objects and add them to schools
major = Major.objects.get(name='Physics')
school = School.objects.get(name='Harvard')
school.majors.add(major)
...or grab schools and add them to majors via reverse-lookup (the "_set" attribute):
school = School.objects.get(name='Harvard')
major = Major.objects.get(name='Physics')
major.school_set.add(school)
Professors would be created without the ManyToMany relationships
school = School.objects.get(name='Harvard')
major = Major.objects.get(name='Physics')
Professor.objects.create(school=school, major=major)
Documentation reference on ManyToManyField: https://docs.djangoproject.com/en/2.1/topics/db/examples/many_to_many/

Related

Add to m2m field on seperate model

Problem:
I have 3 models in 3 separate apps
School
Student
Classroom
class School(models.Model):
Name = models.CharField(max_length=100)
Students = models.ManyToManyField(Student)
class Classroom(models.Model):
School = models.ForeignKey(School, on_delete=models.CASCADE)
Students = models.ManyToManyField(Student)
What I want:
When a new classroom is saved the students that are in that classroom instance are automatically added to the students many-to-many field of the school model.
Then in the school page template I want to display the students in order of the date they were added to the school model, I think this part needs a separate through model but just can't get my head around it.
When a new classroom is saved the students that are in that classroom instance are automatically added to the students many-to-many field of the school model.
There's no such behavior, I can suggest this:
Remove School.Students, then any time you need students of a specific school, you do something along the lines
Students.objects.filter(classroom__School__Name="Hogwarts")
If you want to keep both (for whatever reason) you can create your custom functions that add items into both. Or you can create custom managers to modify default behavior.
Then in the school page template I want to display the students in order of the date they were added to the school model - I think this part needs a separate through model but just can't get my head around it.
You're right, if you want to add any field to a many-to-many you need to define your custom model. Then you can create a many-to-many out of it with through option.
First, I'd like to stick with some pep8 fixes
class School(models.Model):
name = models.CharField(max_length=100)
students = models.ManyToManyField(Student)
class Classroom(models.Model):
school = models.ForeignKey(School, on_delete=models.CASCADE)
students = models.ManyToManyField(Student)
Second, unless you've a reason to set name.max_length to 100, set it to 255.
name = models.CharField(max_length=255)
I'd also set a related name to make things easier
class Classroom(models.Model):
school = models.ForeignKey(School, on_delete=models.CASCADE, related_name='classrooms')
students = models.ManyToManyField(Student)
Now, to your problem.
Your logic is saying that a school can have many students and a student can enroll in many schools (weird but I'll cope), a classroom can exist inside a school and a school can have multiple classrooms, a classroom HAS MANY STUDENTS. This is a key here, you don't need to save the students in the school itself, you can just use something like
class School(models.Model):
name = models.CharField(max_length=100)
students = models.ManyToManyField(Student)
#property
def students(self):
# iterate the class room, return a QS with all students from them
# there're operators for union, but I'm using the function itself
qs = Student.objects.none()
for classroom in self.classrooms: # related name in action
qs = qs.union(classroom.students)
return qs
I'll add a note, the behavior you want can be done in a signal # DON'T DO THING, JUST KNOW IT
More info

What are the benefits of having two models instead of one?

I've a Django model
class Person(models.Model):
name = models.CharField(max_length=50)
team = models.ForeignKey(Team)
And a team model
class Team(models.Model):
name = models.CharField(max_length=50)
Then, I would like to add a 'coach' property which is a one to one relationship to person. If I am not wrong, I have two ways of doing it.
The first approach would be adding the field to Team:
class Team(models.Model):
name = models.CharField(max_length=50)
coach = models.OneToOneField(Person, related_name='master')
The second one would be creating a new model:
class TeamCoach(models.Model):
team = models.OneToOneField(Team)
coach = models.OneToOneField(Person)
Is this right ? is there a big difference for practical purposes ? which are the pro and cons of each approach ?
I will say NEITHER, as every Person has a Team and if every Team has a Coach, it's rather redundant circulation and somewhat unnecessary.
Better to add a field in Person called type directly is more clean and direct, something like:
class Person(models.Model):
# use _ if you care about i18n
TYPES = ('member', 'member',
'coach', 'coach',)
name = models.CharField(max_length=50)
team = models.ForeignKey(Team)
type = models.CharField(max_length=20, choices=TYPES)
Although I would seriously consider refactoring Person to be more generic and get Team to have a ManyToMany to Person... in that case, you can re-use Person in other areas, like Cheerleaders.
class Person(models.Model):
# use _ if you care about i18n
TYPES = ('member', 'member',
'coach', 'coach',)
name = models.CharField(max_length=50)
type = models.CharField(max_length=20, choices=TYPES)
class Team(models.Model):
name = models.CharField(max_length=50)
member = models.ManyToManyField(Person, related_name='master')
Make your models more generic and DRY, they should be easily manageable and not tightly coupled to certain fields (unless absolutely necessary), then the models are more future proof and will not fall under migration nightmare that easily.
Hope this helps.
I can't agree so easy with #Anzel, and since the name of the question is
What are the benefits of having two models instead of one?
I'll try to give my two cents. But before i start i want to place some quotes from the docs.
It doesn’t matter which model has the ManyToManyField, but you should
only put it in one of the models – not both.
Generally, ManyToManyField instances should go in the object that’s
going to be edited on a form. In the above example, toppings is in
Pizza (rather than Topping having a pizzas ManyToManyField ) because
it’s more natural to think about a pizza having toppings than a
topping being on multiple pizzas. The way it’s set up above, the Pizza
form would let users select the toppings.
Basically that's the first thing you should have in mind when creating a M2M relation (your TeamCoach model is that, but more on that in a second) which one is the object holding the relation. What would be more suitable for your problem - choosing a coach for a team when you create it, or choosing a team for a person when you create it? IF you ask me i would prefer the second variant and keep the teams inside of the Person class.
Now lets go to the next section of the docs
Extra fields on many-to-many relationships
When you’re only dealing with simple many-to-many relationships such
as mixing and matching pizzas and toppings, a standard ManyToManyField
is all you need. However, sometimes you may need to associate data
with the relationship between two models.
For example, consider the case of an application tracking the musical
groups which musicians belong to. There is a many-to-many relationship
between a person and the groups of which they are a member, so you
could use a ManyToManyField to represent this relationship. However,
there is a lot of detail about the membership that you might want to
collect, such as the date at which the person joined the group.
For these situations, Django allows you to specify the model that will
be used to govern the many-to-many relationship. You can then put
extra fields on the intermediate model. The intermediate model is
associated with the ManyToManyField using the through argument to
point to the model that will act as an intermediary.
That's actually the answer of your question, having an intermediate model give you the ability to store additional data about the collection. Consider the situation where a coach moves to another team next season, if you just update the M2M relation, you will loose the track of his past teams where he was coaching. Or you will never be able to answer the question who was the coach of that team at year XXX. So if you need more data, go with intermediate model. This is also were #Anzel going wrong, the type field is an additional data of that intermediate model, it's place must be inside it.
Now here is how i would probably create the relations:
class Person(models.Model):
name = models.CharField(max_length=50)
teams = models.ManyToManyField('Team', through='TeamRole')
class Team(models.Model):
name = models.CharField(max_length=50)
class TeamRole(models.Model):
COACH = 1
PLAYER = 2
CHEERLEADER = 3
ROLES = (
(COACH, 'Coach'),
(PLAYER, 'Player'),
(CHEERLEADER, 'Cheerleader'),
)
team = models.ForeignKey(Team)
person = models.ForeignKey(Person)
role = models.IntegerField(choices=ROLES)
date_joined = models.DateField()
date_left = models.DateField(blank=True, null=True, default=None)
How will I query this? Well, I can use the role to get what type of persons I'm looking for, and I can also use the date_left field to get the current persons participating in that team right now. Here are a few example methods:
class Person(models.Model):
#...
def get_current_team(self):
return self.teams.filter(teamrole__date_left__isnull=True).get()
class Team(models.Model):
#...
def _get_persons_by_role(self, role, only_active):
persons = self.person_set.filter(teamrole__role=role)
if only_active:
return persons.filter(teamrole__date_left__isnull=True)
return persons
def get_coaches(self, only_active=True):
return self._get_persons_by_role(TeamRole.COACH, only_active)
def get_players(self, only_active=True):
return self._get_persons_by_role(TeamRole.PLAYER, only_active)

django: need to design models/forms for a 'multiple level nested' structures

Assume some Company with Employees. There are Name and Contact information bound to each Employee. Each Contact contains Street and Phones fields.
What I want is a page which lists employees within a company. But everything must be listed as forms. Because I want to be able to modify the particular Employee information and the most important - I want to be able to add new Employees (clicking a button "Add new employee" must add a new empty "Employee form"). As well as it must allow to add a new phone number to the existing Employee's Contact information any time.
The data model looks like:
--Company
----Employee1
------Name
------Contact
--------Street
--------Phones
----------Phone1
----------Phone2
----Employee2
------Name
------Contact
--------Street
--------Phones
----------Phone1
----------Phone2
----------Phone3
...
Could someone please help to design Models and Forms for such a task? Your help is very much appreciated. Many thanks!
P.S. Forgot to mention that I want all the data "collected" in the Company object at the end of the day. I mean when I serialize c = Comapany.objects.all()[0] on the back end the entire employee information must be visible, like c.employees[0].contact.phones[0] must be the first employee's first phone number. Thanks.
P.P.S.
That is not the case that I'm just forwarding my project. This is just an hypothetical example I'd created to present the problem. I'm a django newbie and trying to understand how the framework gets things rolling.
I've spent lot of time on this. I've found several ways to go, but no one got me to the end. For instance, a wonderful blog about nested formsets http://yergler.net/blog/2013/09/03/nested-formsets-redux/ helped with forms and rendering. But, it solved only the half of the problem. The data like I mentioned above is not "being collected" into an object. At the end of the day I want to serialize a Company object and save it in yaml format using pyyaml (see my previous post django: want to have a form for dynamically changed sequence data).
Django is perfect with "static" models and forms, ModelForms are awesome. But what if your model needs to be changed dynamically? No standard way to go. Either no appropriate documentation nor I could find a one. Thus, I'd like to hear how experts imagine the solution for such a problem.
Try this:
from django.db import models
class _Contact(object):
pass
class Company(models.Model):
name = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
#property
def employees(self):
return self.employee_set.prefetch_related('phones').order_by('-created_at')
class Phone(models.Model):
number = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now_add=True)
class Employee(models.Model):
name = models.CharField(max_length=255)
street = models.CharField(max_length=255)
phones = models.ManyToManyField('Phone', through='EmployeePhone', blank=True)
created_at = models.DateTimeField(auto_now_add=True)
company = models.ForeignKey(Company)
#property
def contact(self):
_contact = _Contact()
_contact.street = self.street
_contact.phones = self.phones.order_by('-employeephone__created_at')
return _contact
class EmployeePhone(models.Model):
employee = models.ForeignKey(Employee)
phone = models.ForeignKey(Phone)
created_at = models.DateTimeField(auto_now_add=True)
However, you should just use employee.street and employee.phones. employee.contact is redundant.

Database structure in Django for voting app

I'm trying to wrap my head around how I would structure my database tables in the Django webapp I'm writing. I'm a relative newbie to web development, but this is the very first time I've tried to use a database, so bear with me if it's a stupid question.
The webapp goes through each Oscar the Academy gives out and allows the user to select which of some (varying) number of nominations will win an Oscar. The data from each individual session will be publicly available by going to a url like [url].com/answers/[unique id]. The overall data will also be available on a results page. So I've started writing my models file, and this is what I have so far:
from django.db import models
class Nominee(models.Model):
award = models.CharField(max_length=50)
title = models.CharField(max_length=50)
key = modelsCharField(max_length=50)
subtitle = models.CharField(max_length=50)
numVotes = models.IntegerField()
class Session(models.Model):
id = models.IntegerField() # unique id of visitor
bpictureVote = models.ForeignKey(Nominee, related_name = 'nom')
bactorVote = models.ForeignKey(Nominee, related_name = 'nom')
# ... for each award
I was originally thinking of having
class Award(models.Model):
name = models.CharField(max_length=50)
and at the beginning of Nominee,
award = models.ForeignKey(Award, related_name = 'award')
but I couldn't figure out why that would be better than just having award be a part of the Nominee class.
This is really just a start, because I've gotten a bit stuck. Am I on the right track? Should I be doing this totally differently (as I probably should...)? Any thoughts?
Thanks!
You are on the right track.
You need a separate Award class to avoid having to type in award's name every time you create a Nominee. By having a ForeignKey reference you make sure that you can safely rename your award, add additional information about the award (let's say in the future you decide to give each award a separate page with a description and a list of nominees), you also avoid errors which can happen from having a set of different spellings and typos ("Best Engineer Award" and "Best Engineer award"). It also makes sense - your application operates a set of objects: user sessions, nominees and awards.
Few unrelated notes:
You don't need an explicit Session.id field, django ORM creates it for you.
Property names have to be name_with_underscores, not camelCase.
No spaces around "=" in an arguments list: models.ForeignKey(Nominee, related_name='nom').
4 spaces instead of 2 (unless explicitly otherwise specified).
I am not entirely sure, because you do mention multiple nominees per award (assuming this is something like a poll before the actual nomination) a ManyToMany would be your required relation, in order to use also the additional user data.
But in the case you have implemented this as a specific app for nominations and implemented a custom user model then this would be refactored to something else...
Anyway to your current implementation:
class Nominee(models.Model):
title = models.CharField(max_length=50)
key = modelsCharField(max_length=50)
subtitle = models.CharField(max_length=50)
...
class Award(models.Model):
name = models.CharField(max_length=50)
nominees = models.ManyToManyField(Nominee, through='AwardNominees')
...
class AwardNominees(models.Model):
nominee = models.ForeignKey(Nominee)
award = models.ForeignKey(Award)
user = models.ForeignKey(User)
numVotes = models.IntegerField()
....
So it turned out I was thinking about this entirely wrong. I've now completely changed things, and now it's fully functional (!). But in the spirit of full disclosure, I should say that it definitely may not be the best solution. It sure seems like a good one, though, because it's really simple. Now I have only one model:
class Vote(models.Model):
award = models.CharField(...) # Name of the award
title = models.CharField(...) # Title of the nominee
subtitle = models.CharField(...) # Subtitle of the nominee
uid = models.CharField(...) # A 6 character user ID for future access
When I want to show the results of one user's votes, I can use Django's database tools to filter for a certain uid captured in the URL. When I want to tally the votes, I can use a combination of filters and Django's count() to determine how many votes each nominee had for a certain award. Sounds reasonable enough to me!

Django: convert ManyToManyField to ForeignKey

Let's supposed I created two models:
class Car(models.Model):
name = models.CharField(max_length=50)
size = models.IntegerField()
class Manufacturer(models.Model):
name = models.CharField(max_length=50)
country = models.CharField(max_length=50)
car = models.ManyToManyField(Car)
I added entries to both models, then I realized that each Car was only related to a unique Manufacturer. So, I should convert my ManyToManyField to a ForeignKey:
class Car(models.Model):
name = models.CharField(max_length=50)
size = models.IntegerField()
manufacturer = models.ForeignKey(Manufacturer)
class Manufacturer(models.Model):
name = models.CharField(max_length=50)
country = models.CharField(max_length=50)
How can I do that without losing my entries? I tried to look in South documentation but I did not found this way of conversion...
This is nontrivial, I think you will need three migrations:
Add the ForeignKey.
Convert the ManyToMany to ForeignKey (using the forwards method).
Remove the ManyToMany.
You could possibly merge 1 and 2 or 2 and 3 together, but I wouldn't recommend it.
Additionally, you should also implement a backwards method for 2.
An example for 2.'s forwards would be:
class Migration(SchemaMigration):
def forwards(self, orm):
for manufacturer in orm.Manufacturer.objects.all():
for car in manufacturer.car.all():
car.manufacturer = manufacturer
car.save()
Please note that:
There is no backwards method here yet.
This needs to be tested extensively: a migration is something you should be extra careful about.
In case a car has two manufacturers, the last one will be kept.
This is very inefficient, we do a Query per car per manufacturer!
You will also need to update the code that uses those relationships in step 2. / 3.
Based on the excellent answer provided by Thomas Orozco, I'd like to provide the solution for Django>=1.7 (basically the point 2 Convert the ManyToMany to ForeignKey is what varies in newer versions of Django). So here the code for the second migration:
class Migration(migrations.Migration):
def migrate_m2m_to_fk(apps, schema_editor):
Manufacturer = apps.get_model("app", "Manufacturer")
for manufacturer in Manufacturer.objects.all():
for car in manufacturer.car.all():
car.manufacturer = manufacturer
car.save()
def migrate_fk_to_m2m(apps, schema_editor):
Car = apps.get_model("app", "Car")
for c in Car.objects.all():
if c.manufacturer:
c.manufacturer.car.add(c)
c.manufacturer.save()
operations = [
migrations.RunPython(migrate_m2m_to_fk, migrate_fk_to_m2m)
]
Where "app" is the Django app where the models live. Both forward and reverse migration code is shown (as Thomas mentions, running this migrations may incur in loss of data, if more than one relationship was present beforehand, so please take care).

Categories