1

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.

1
  • Just an observation: you don't need to define both model and queryset in your DetailView; one is enough.
    – Ralf
    Apr 17, 2018 at 0:09

2 Answers 2

1

If I understand your question correctly, then what you want should be possible.

class JournalDetailView(DetailView):
    context_object_name = 'journal'
    model = Journal
    template_name = 'journal_detail.html'

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)

        # rates related to the current journal
        ctx['rates_list_1'] = self.object.rate_set.all()

        # rates related to the current journal and a specific product
        # that has been past to the request as an query string parameter
        some_product = get_object_or_404(
            Product, pk=self.request.GET.get('product_id'))
        ctx['rates_list_2'] = self.object.rate_set.filter(product=some_product)

        return ctx

This code sample takes advantage of the get_context_data method to add variables to your template context.

3
  • some_product should be an Product instance; Maybe you could use Product.objects.get(pk=1) or get_object_or_404(Product, pk=1) or something similar to get the instance of which you want to get the rates.
    – Ralf
    Apr 17, 2018 at 15:46
  • Thanks, @Ralf! I was able to get the frequency, price, and journal from rate.Rate in the DetailView using the rate_list_1 ctx. I understand using get_context_data for the ctx['rate_list_1'] = self.object.rate_set.all(). However, I am curious to see an example of the type of logic needed set some_product = to. I wasn't able to find an example where the variable is dynamic based on the data selected vs a variable that is hard coded. i.e.***some_product = Product.object.filter(id=1)***. This is primarily why I was not able to make the second context dict work. Any thoughts?
    – Cam Shan
    Apr 17, 2018 at 15:53
  • Updated my answer: you could pass the product_id as a query string parameter to the request.
    – Ralf
    Apr 17, 2018 at 16:03
1

Is it possible to import the Product and Rate models into your Journal app and then filter the Products by their relation to a Journal, then find the associated Rate object?

1
  • Hi @Alister, thanks for the input - I did think about that approach, however, each product has 6 different rates based on the Frequency model so it ends up being less expensive and confusing on the admin side to make the connection in the Rate app.
    – Cam Shan
    Apr 17, 2018 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.