Multiple Optional ForeignKeys in Django - python

Before I start: My understanding of Django is at a beginner Level and i could not find adequate help through google.
I'll start with an example:
class Player(models.Model):
...
class Tournament(models.Model):
...
first_place = models.ForeignKey(Player)
second_place = models.ForeignKey(...)
third_place = models.ForeignKey(...)
My problem is: there are multiple people in first place, second place and so on. How can I realize the model in a way which lets me add my own number of Players every time?
I already tried ManyToMany instead of ForeignKey but then I get an Error in the admin menu when i try to save a Tournament Object stating that there has to be an ID present for the Object even when I do not select any Players to be added.

Don't know if I understand the question correctly, but if you want to make ForeignKeys optional and also want to add multiple Players, you could use ManyToManyField and set null and blank, both True:
class Tournament(...):
...
first_place = models.ManyToManyField(Player, blank=True, null=True)
...

This would solve your problem you can now freely add each and any place to each player. The .count() function in your view can get the number of objects for the selected players. No need for a manytomanyfield when you can just assign a place to an object by the user with every user now being able to have unlimited places in each category, if I understand what your trying to do here. Comment if you need more help.
Class FirstPlace(models.Model):
first = models.ForeignKey(Player)
Class SecondPlace(models.Model):
second = models.ForeignKey(Player)
Class ThirdPlace(models.Model):
third = models.ForeignKey(Player)

Related

Swapping Inlines (and dynamic Model creation)

A game, and lets say there are different (restrictive) settings for recreation and I want to adjust via django admin.
models.py:
class Board(models.Model):
name = CharField()
class Player(models.Model):
bord = Foreignkey('Board')
max_cards = IntegerField()
class Card_Slot(models.Model):
owner = Foreignkey('Player')
card = CharField()
A Board should have a random number of Players associated with it. An inline TabularInline should be used to add or delete Players, with an IntegerField for the max number of Cards (max_cards).
I use signals on save to create dynamically Card_Slots appropriate to the number of max_cards.
--- thats what I have, now what I want ---
'Swapping' the Inline after setting the max_cards, for an Inline that represents the Players and their Cards (thus Playername and card in Card_Slot should be editable).
Several headaches come up for me on that. If possible I don't want to overwrite admin templates. If I should, I would appreciate a hint that goes further then pointing to the docs, meaning pointing in a direction on good practice.
I'm also not sure, if I should use one Model for Player and Card_Slot. (This would make the edit name thing easier, but the dynamic size of Card_Slots worse).. I think.
I have no clue on how I should do that. Pls help.
(One approach appears to be with custom Fieldsets, which I didn't get to work so far. The thought is to replace the content and type of the Integer Field with a ModelMultipleChoiceField related to a queryset of the Card_Slots. But I dont understand how I can do custom Fields on an Inline without a model etc.)

Django still stuck (Many to Many)

the idea is that students can send a ping to their teacher if they do not understand something, but first I am trying to get the student, teacher and whatever class they are in relationship
so in my models after a few attempts I have come up with this
Models
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=50)
class Teacher(models.Model):
name = models.CharField(max_length = 70)
class Lecture(models.Model):
name = models.CharField(max_length = 70)
members = models.ManyToManyField(
Student,
through = 'part_of_class',
through_fields = ('lecture', 'student'),
)
lecturers = models.ManyToManyField(
Teacher,
through = 'Teacher_of_class',
through_fields = ('lecture', 'teacher'),
)
class Teacher_of_class(models.Model):
lecture = models.ForeignKey(Lecture)
teacher = models.ForeignKey(Teacher)
class part_of_class(models.Model):
lecture = models.ForeignKey(Lecture)
student = models.ForeignKey(Student)
Question is adding a student field messes up, and Im not even sure this is the right way, it sounds right in my head but I am sure I am missing something, any clues?
I'm not entirely sure that you need explicit intermediary relationships (Teacher_of_class and part_of_class). Instead, you should try utilizing fields within the other models to do what you want. For example, will a lecture generally have more than one teacher, or just one? You could model either of those with a field in Lecture.
Edit: Apologies, I misread your code initially with regards to the fields. My suggestion is to not use an additional relationship class unless you know why you need it.
If you just want to have a lecture that can have multiple students and multiple lecturers, you do not need to have the intermediate models, Django will handle this for you if you don't specify any. I would only use the intermediate tables if you wanted to store something with that relationship.
Also when trying to add your student and teacher objects to the lecture model, be sure to save them.
Your save code should look something like this:
aStudent = Student(name="Taco")
aTeacher = Teacher(name="Burrito")
aStudent.save()
aTeacher.save()
aLecture = Lecture(name="Cooking With Python!")
aLecture.members.add(aStudent)
aLecture.lecturers.add(aTeacher)
aLecture.save()
Hope this helps!

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.

Django: Grouping and ordering across foreign keys with conditions

I have some Django models that record people's listening habits (a bit like Last.fm), like so:
class Artist(models.Model):
name = models.CharField()
class Song(models.Model):
artist = models.ForeignKey(Artist)
title = models.CharField()
class SongPlay(models.Model):
song = models.ForeignKey(Song)
user = models.ForeignKey(User)
time = models.DateTimeField()
class User(models.Model):
# doesn't really matter!
I'd like to have a user page where I can show the top songs that they've listened to in the past month. What's the best way to do this?
The best I've come up with so far is:
SongPlay.past_month
.filter(user=user)
.values('song__title', 'song__id', 'song__artist__name')
.annotate(plays=Count('song'))
.order_by('-plays')[:20]
Above, past_month is a manager that just filters plays from the last month. Assume that we've already got the correct user object to filter by as well.
I guess my two questions are:
How can I get access to the original object as well as the plays annotation?
This just gives me certain values, based on what I pass to values. I'd much rather have access to the original object – the model has methods I'd like to call.
How can I group from SongPlay to Artist?
I'd like to show a chart of artists, as well as a chart of songs.
You can use the same field in both values and annotate.
You have the primary key of the Song object (you could just use song instead of song__id), so use
Song.objects.get(id=...)
For your second question, do a separate query with song__artist as the field in values and annotate:
from django.db.models import Count
SongPlay.past_month
.filter(user=user)
.values('song__artist')
.annotate(plays=Count('song__artist'))
.order_by('-plays')[:20]
agf has already showed you how to group by song_artist. What I would do to get the actual Song object is store it in memcached, or if the method you are calling is rather simplistic make it a static method or a class method. You might could also initialize a Song object with the data from the query and not actually save it to get access to this method. Might help to know the details of the methods you want to call from the Song object.

Categories