Quick question, I can not for the life of me figure out how to get this to work and need some help.
Problem I need to be able to query another model's objects to use for choices in a different model. I was thinking about a foreign key but I really do not need it to extend the other model.
I have a group called Group
that will display choices from another model called Games
. These objects will already be saved in the database. I just can't figure out how to display the choices.
Models.py for the Model we are trying to display the choice in
game
is the field we want the choices from the Games
model
from django.db.models.aggregates import Max
from games.models import Games
# Create your models here.
class Group(models.Model):
name = models.CharField(max_length=200)
game = models.ForeignKey(Games, on_delete=models.CASCADE)
size = models.IntegerField()
total_size = models.CharField(max_length=200)
play_time = models.DateTimeField()
description = models.TextField(max_length=200)
roles = models.CharField(max_length=200)
is_full = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
Models.py from the Model we want to generate the choices from
I want to use the name
field from this model for the choices on the Group
game
field.
from django.db import models
from django.db.models.aggregates import Max
# Create your models here.
class Games(models.Model) :
GENRE_CHOICES = [
('Action', 'Action'),
('Acion-Adventure', 'Action-Adventure'),
('Adventure', 'Adventure'),
('MMO', 'MMO'),
('Puzzle', 'Puzzle'),
('Role Playing', 'Role Playing'),
('Simulation', 'Simulation'),
('Strategy', 'Strategy'),
('Sports', 'Sports')
]
RATING_CHOICES = [
('E', 'Everyone'),
('E10+', 'Everyone 10+'),
('T', 'Teen'),
('M', 'Mature 17+'),
('A', 'Adults Only 18+'),
('RP', 'Rating Pending')
]
PLATFORM_CHOICES = [
('Multi', 'Multi Platform'),
('PC', 'PC'),
('XBOX', 'XBOX'),
('Playstation', 'Playstation'),
('Nintendo', 'Nintendo')
]
name = models.CharField(max_length=200)
platform = models.CharField(max_length=20,
null=True,
choices=PLATFORM_CHOICES,
default='Select'
)
publisher = models.CharField(max_length=100)
genre = models.CharField(max_length=100,
null=True,
choices=GENRE_CHOICES,
default='Select'
)
rating = models.CharField(max_length=15,
null=True,
choices=RATING_CHOICES,
default='Select'
)
release_date = models.DateField()
tags = models.CharField(max_length=200)
picture = models.ImageField(
max_length=200,
default='games/default.png',
null=True,
upload_to='games/'
)
is_new = models.BooleanField(null=True)
is_popular = models.BooleanField(null=True)
is_featured = models.BooleanField(null=True)
def __str__(self):
return self.name