简体   繁体   中英

Calling a foreign key relationship in an “if” statement in Django template

I have two models, Booking and Confirmation that are related via a ForeignKey relationship through "booking." I want to only display bookings in my detail view that have an attribute value of is_confirmed ==True. I don't really want a queryset, I just want to display the booking information from the "Booking" model if the confirmation is True in the template.

models.py:

class Booking(models.Model):
    user = models.ForeignKey(CustomUser, null=True, default='', on_delete=models.CASCADE)
    expert = models.ForeignKey(CustomUser, null=True, default='',on_delete=models.CASCADE, related_name='bookings')
    title = models.CharField(max_length=200, default='Video call with ..', null=True)
    start_time = models.DateTimeField('Start time', null=True)
    end_time = models.DateTimeField('End time', null=True)
    notes = models.TextField('Notes', blank=True, null=True) 

    class Meta:
        verbose_name = 'Booking'
        verbose_name_plural = 'Bookings'

    def get_absolute_url(self):
        return reverse('booking:booking_detail', kwargs={"pk": self.pk})

class Confirmation(models.Model):
    booking = models.ForeignKey(Booking, on_delete=models.CASCADE)
    expert_confirming = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
    is_confirmed = models.BooleanField(default=False)

    def get_absolute_url(self):
        return reverse('booking:booking_detail', kwargs={"pk": self.booking_id})

views.py:

class BookingDetailView(DetailView):
    model = Booking
    template = 'templates/booking_detail.html'

booking_detail.html:

<div class="container" id="booking_content">
    <p>{{ booking.title }}</p>
    <p>{{ booking.start_time }}</p>
    <p>Booking request by: {{ booking.user }}</p>
    <p>Expert requested: {{ booking.expert }}</p></div>

I'm not sure how the if statement in the template should reference these related models to display what I want.

I think that with the way you have your models you would have to run a query on the Confirmation model to determine whether there exists a confirmation for a particular booking. But why have a separate confirmation model at all? Try just moving the relevant field into the booking model:

class Booking(models.Model):
    user = models.ForeignKey(CustomUser, null=True, default='', on_delete=models.CASCADE)
    expert = models.ForeignKey(CustomUser, null=True, default='',on_delete=models.CASCADE, related_name='bookings')
    title = models.CharField(max_length=200, default='Video call with ..', null=True)
    start_time = models.DateTimeField('Start time', null=True)
    end_time = models.DateTimeField('End time', null=True)
    notes = models.TextField('Notes', blank=True, null=True)
    is_confirmed = models.BooleanField(default=False) # just this field since you already have an expert.

That simplifies things, and puts a little less load on your db. Then, you can show only bookings that are confirmed with this template language:

{% if booking.is_confirmed %}

    <div class="container" id="booking_content">
        <p>{{ booking.title }}</p>
        <p>{{ booking.start_time }}</p>
        <p>Booking request by: {{ booking.user }}</p>
        <p>Expert requested: {{ booking.expert }}</p>
    </div>

{% else %}
    ...
{% endif %}

You may have a good reason for having a separate confirmation model. If so, then this answer is irrelevant. If not, then maybe this could help simplify things for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM