Reputation: 25
I'm new to Django but I'm trying to replace the categories constant with the types of videos from the other model, VideoTypes. How would I do this?
from django.db import models
# from django.contrib.auth import get_user_model
# User = get_user_model()
CATEGORIES = (
('Executive Speaker Series', 'Executive Speaker Series'),
('College 101', 'College 101'),
('Fireside Chat', 'Fireside Chat'),
('Other', 'Other')
)
# Create your models here.
class VideoType(models.Model):
type = models.CharField(max_length=255)
def __str__(self):
return self.type
class Video(models.Model):
title = models.CharField(max_length=255)
link = models.CharField(max_length=1000)
category = models.CharField(choices=CATEGORIES, max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
# owner = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.title
Upvotes: 0
Views: 43
Reputation: 373
The implementation that you have, requires tuples with variables instead of strings.
Change your CATEGORIES
variable to
CATEGORIES = (
(Executive_Speaker_Series, 'Executive Speaker Series'),
(College_101, 'College 101'),
(Fireside_Chat, 'Fireside Chat'),
(Other, 'Other')
)
Upvotes: 0
Reputation: 9359
you're looking for ForeignKey field. It also needs to have set behaviour how it should behave when the related model is deleted, see on_delete description.
VideoType(models.Model):
type = models.CharField(max_length=255)
def __str__(self):
return self.type
class Video(models.Model):
title = models.CharField(max_length=255)
link = models.CharField(max_length=1000)
category = models.ForeignKey(VideoType, on_delete=models.SET_NULL) # category is now referring to VideoType model
timestamp = models.DateTimeField(auto_now_add=True)
Upvotes: 1