So I've created a little database relationship for the question:
from django.db import models
# Create your models here.
class Poll(models.Model):
title = models.CharField(max_length=255)
date_created = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.title
class Option(models.Model):
title= models.CharField(max_length=50)
poll = models.ForeignKey(Poll, on_delete=models.CASCADE)
desc = models.CharField(max_length=255)
def __str__(self):
return self.title
class Voter(models.Model):
name = models.CharField(max_length = 255)
option = models.ForeignKey(Option, on_delete=models.CASCADE)
def __str__(self):
return f'{self.name} - {self.option}'
So I have 2 questions:
Is it possible to access the value of
Optionmodel'sdescfield fromVotermodel's__str__function ?Is it possible to access the value of
Pollmodel'sdate_createdfield fromVotermodel's__str__function ?
Thank you so much!