I've little problem with django models. I'm trying to create a game statistics service, but can't figure out how could I pick players from each team as a starting roster (doesn't contain all of the team players) of the game. Picking should work so that home and visitor has separated player lists. And then it should be possible to create eg Goal object where I could pick player as scorer only from the starting roster. I've already tried to put home and visitor players as ManyToManyField in the Game model, but it won't work. It needs some filtering at least.
class Game(models.Model):
series = models.ForeignKey(Series, related_name="game_series")
date = models.DateTimeField(default=None, blank=True, null=True)
home = models.ForeignKey(Team, related_name="game_home")
visitor = models.ForeignKey(Team, related_name="game_visitor")
class Goal(models.Model):
game = models.ForeignKey(Game, related_name="goal_game")
minutes = models.PositiveIntegerField(default=None, blank=True, null=True)
seconds = models.PositiveIntegerField(default=None, blank=True, null=True)
scorer = models.ForeignKey(Player, related_name="goal_scorer")
assist = models.ForeignKey(Player, related_name="goal_assist")
class Save(models.Model):
game = models.ForeignKey(Game, related_name="save_game")
player = models.ForeignKey(Player, related_name="save_player")
count = models.PositiveIntegerField(default=None, blank=True, null=True)
The question is: how should I modify models to achieve the desired result? Note: I'm using admin views for data input, so the proposed resolution should work from there.
EDIT: There is also Player model which I forgot to explain earlier. So, both of teams has already defined list of players where to pick roster for the game.
class Player(models.Model):
team = models.ForeignKey(Team, on_delete=models.PROTECT, related_name="player_team")
firstname = models.CharField(max_length=200, default=None, blank=True, null=True)
lastname = models.CharField(max_length=200, default=None, blank=True, null=True)
number = models.IntegerField(default=None, blank=True, null=True)
description = models.TextField(default=None, blank=True, null=True)
Related
class City(models.Model):
name = models.CharField(max_length=200, null=True, blank=False)
class District(models.Model):
city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
name = models.CharField(max_length=200, null=True, blank=False)
class ShippingAddress(models.Model):
city = models.ForeignKey(City, on_delete=models.CASCADE, null=False)
district = models.ForeignKey(District, on_delete=models.CASCADE, null=False)
I would like to filter the admin panel by city (selected city). in order to filter too much similar districts names.
If someone has another way to do it no problem, that was a junior try, waiting for your advice.
I'm writing the management interface for a competition, and have the following models:
class Heat(models.Model):
current_heat = models.IntegerField()
track = models.ForeignKey(Track, on_delete=models.PROTECT)
previous_heat = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)
start_date = models.DateTimeField('Start Date', default=timezone.now)
end_date = models.DateTimeField('End Date', null=True, blank=True)
class Game(models.Model):
heat = models.ManyToManyField(Heat)
format = models.TextField(max_length=20, default="V")
player1 = models.ForeignKey(User, on_delete=models.PROTECT, related_name='player1')
player2 = models.ForeignKey(User, on_delete=models.PROTECT, related_name='player2')
winner = models.ForeignKey(User, on_delete=models.PROTECT, related_name='winner', null=True, blank=True)
Now I want to get the standings for a specific heat. In other words, I want to get all unique players in that heat, together with how many wins they got (thus where they are in Game.winner), and how many losses they got (thus where Game.winner is not that player but it is not Null, and they are in either Game.player1 or Game.player2.)
Ideally, this should then even be ordered by wins (descending), losses (ascending).
I've been looking through the aggregation docs from Django, but I don't see how to start since I need to get so many different things all combined.
Hi I'm new to programming and I am building a recipe website to learn, what I am struggling with though is how to handle recipe ingredients? I would like to do the following:
Have global recipe ingredients i.e. common ingredients, chicken, beef etc.
Allow users to create their own ingredients (for that user only) i.e. big tomato
Attach ingredients to a recipe regardless of if they are global or user created
Allow users to add ingredients to their pantry and how much of the ingredient they have in stock
What I think the models would like is below, but I'm not sure if this is correct or the best method, any advice appreciated.
recipe/models.py
User = settings.AUTH_USER_MODEL
class Recipe(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
recipeName = models.CharField(max_length=220) # grilled chicken pasta
userRecipeIngredients = models.ManyToManyField(UserCreatedIngredient, blank=True, Null=True, through='IngredientToRecipe')
globalRecipeIngredients = models.ManyToManyField(GlobalIngredient, blank=True, Null=True, through='IngredientToRecipe')
class UserCreatedIngredient(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=220) # grilled chicken
class GlobalIngredient(models.Model):
name = models.CharField(max_length=220) # chicken
class IngredientToRecipe(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
userIngredient = models.ForeignKey(UserCreatedIngredient, on_delete=models.SET_NULL)
globalIngredient = models.ForeignKey(GlobalIngredient, on_delete=models.SET_NULL)
quantity = models.CharField(max_length=50, blank=True, null=True) # 400
unit = models.CharField(max_length=50, blank=True, null=True) # pounds, lbs, oz ,grams, etc
instructions = models.TextField(blank=True, null=True) # chopped, diced etc.
pantry/models.py:
from recipes.models import IngredientToRecipe
User = settings.AUTH_USER_MODEL
class pantryIngredients(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
ingredients = models.ForeignKey(IngredientToRecipe, on_delete=models.SET_NULL)
inStock = = models.IntegerField(default=0, blank=False) # user can increment i.e. two chicken in stock
One way to address the first three requirements in your question would be to build inheritance into your models.
class Recipe(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
recipeName = models.CharField(max_length=220) # grilled chicken pasta
ingredients = models.ManyToManyField(Ingredient, blank=True, Null=True, through='IngredientToRecipe')
class Ingredient(models.Model):
name = models.CharField(max_length=220)
class GlobalIngredient(Ingredient):
pass
class UserCreatedIngredient(Ingredient):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
class IngredientToRecipe(models.Model):
ingredient = models.ForeignKey(Ingredient, on_delete=models.SET_NULL)
quantity = models.CharField(max_length=50, blank=True, null=True) # 400
unit = models.CharField(max_length=50, blank=True, null=True) # pounds, lbs, oz ,grams, etc
instructions = models.TextField(blank=True, null=True) # chopped, diced etc.
By allowing this inheritance to take place you can improve data redundancy.
I am trying to implement a grid layout for entering marks of students into my model. The model is described as:
class User(AbstractUser):
is_student = models.BooleanField(default=False)
is_teacher = models.BooleanField(default=True)
email = models.EmailField('email address', blank=False)
first_name = models.CharField(max_length=50, blank=False)
last_name = models.CharField(max_length=50, blank=False)
class Student(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, related_name='student_profile')
yoe = models.IntegerField('year of enrollment', choices=YEAR_CHOICES,
default=datetime.datetime.now().year)
photo = models.ImageField(upload_to='student/%Y/')
class Paper(models.Model):
semester = models.CharField(max_length=1)
name_of_paper = models.CharField(max_length=200)
max_pass_marks = models.IntegerField("Max Marks",validators=[MaxValueValidator(99)])
class Test(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, default='')
paper = models.ForeignKey(Paper, on_delete=models.CASCADE, default='')
dot = models.DateField("Date of Test")
marks = models.IntegerField("Marks Obtained",validators=[MaxValueValidator(99)], default=0)
I want to be able to create a form with a grid layout where each row of the grid will have the students name followed by text boxes where users can enter the test scores obtained by students in each of the 'Paper' they have opted for. Obviously the test scores should then populate the 'Test' table/model. I am relatively new to django and cant really wrap my head around this. Any help will be appreciated.
I have the following model:
class Trip(models.Model):
driver = models.ForeignKey(User)
trip_cost = models.IntegerField(blank=True, null=True)
passenger = models.CharField(max_length=50, blank=True, null=True)
shared_cost = models.IntegerField(default=0, blank=True, null=True)
Each Trip can have a driver alone, or a driver with several passenger. For each passenger the driver can set the percentage of the trip_cost each passenger is going to pay.
What I need is to have:
the field passenger to be listing all Users
several passenger + shared_cost for each Trip.
I guess I should use Many-to-many but I cannot make it work. Also when I try to set the passenger to models.ForeignKey(User), I get an error.
Any help or direction highly appreciated.
Use related_name when refer to same models using 2 fields.
class Trip(models.Model):
driver = models.ForeignKey(User,on_delete=models.CASCADE, related_name='driver')
trip_cost = models.IntegerField(blank=True, null=True)
passenger = models.ForeignKey(User,on_delete=models.CASCADE, related_name='passenger')
shared_cost = models.IntegerField(default=0, blank=True, null=True)