I have 3 apps in my Django (1.11) project:
My journal app has a Journal
model with a ForeignKey
field connected to my rate app. My product app is also connected to the rate app by a ForeignKey
extending from the Product
model.
Here is the code for the three models:
# journal app
class Journal(models.Model):
name = models.CharField(_('Name'), max_length=255)
...
def __str__(self):
return self.name
# product app
class Product(models.Model):
name = models.CharField(_('Name'), max_length=255)
...
def __str__(self):
return self.name
# rate app
from journal.models import Journal
from product.models import Product
class Frequency(models.Model):
frequency = models.CharField(max_length=20)
def __str__(self):
return self.frequency
class Rate(models.Model):
price = models.DecimalField(max_digits=10, decimal_places=2)
frequency = models.ForeignKey(Frequency)
product = models.ForeignKey(Product)
journal = models.ForeignKey(Journal)
def __str__(self):
return str(self.price)
I can define a price
in the Rate
model based on the Product.name
, Journal.name
, and the Frequency.frequency
. Since the rate app allows me to set the price for each product based on the frequency and journal, I want to display that information back in my journal app within my template being rendered by this view:
class JournalDetailView(DetailView):
context_object_name = 'journal'
model = Journal
queryset = Journal.objects.all()
template_name = 'journal_detail.html'
This way all journal-specific information in journal_detail.html
MY QUESTION:
Is accessing the rate data that is unique to a specific product and journal something I can access back inside by journal app? I know adding another ForeignKey from Rate
to Journal
will throw an error but I cannot think of any other logic to accomplish this task.
model
andqueryset
in yourDetailView
; one is enough.