Manish Ojha
Manish Ojha

Reputation: 515

Get data from the model class in Django

I have a DetailView of a specific channel. All the data of the channel is listed here. There is a model class named 'ExecutionLog'. Currently, all others data are shown in the detail view except the data from the 'ExecutionLog' model class.

The class is in 'Class-based views' which is totally new to me. I could not understand what is really going on.

DetailView class view:

class ChannelDetailView(LoginRequiredMixin, SuperAdminMixin, ChannelView, DetailView):
    def get_context_data(self, *args, **kwargs):
        context = super(ChannelDetailView, self).get_context_data(*args, **kwargs)

        if 'date' in self.request.GET:
            d = self.request.GET['date']
            date = datetime.datetime.strptime(d, "%Y-%m-%d").date()
        else:
            date = datetime.date.today()
        context['activePage'] = {'tree': 'ChannelPage', 'branch': 'index'}
        datas = get_datas_hourly(True, self.object.id, date)
        matches_data = get_datas_hourly(False,self.object.id, date)
        context['date'] = date
        context['labels'] = datas.keys()
        context['values'] = datas.values()
        context['matches_labels'] = matches_data.keys()
        context['matches_values'] = matches_data.values()
        return context

model for ExecutionLog:

class ExecutionLog(models.Model):
    ACTION_TYPES = (
        (0, 'START'),
        (1, 'STOP'),
        (2, 'ASSIGNED'),
        (3, 'ERROR'),
        (4, 'OTHERS'),
    )
    type = models.IntegerField(default=0, choices=ACTION_TYPES)
    title = models.CharField(max_length=255)
    description = models.TextField(max_length=255, blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True)
    execution = models.ForeignKey(Execution, related_name="execution_logs")

    class Meta:
        ordering = ['-date']

I want to get the ExecutionLog data of the specific channel the detailview page is of.

Upvotes: 0

Views: 1338

Answers (1)

zaidfazil
zaidfazil

Reputation: 9245

You may need to override your get() method,

def get(self, request, *args, **kwargs):
    self.object = self.get_object()
    context = self.get_context_data(object=self.object)
    return self.render_to_response(context)

You could edit the view as your requirements. Also, take a look at the definition of DetailView before anything.

EDIT:

def get_context_data(self, *args, **kwargs):
    context = super(ChannelDetailView, self).get_context_data(*args, **kwargs)

    if 'date' in self.request.GET:
        d = self.request.GET['date']
        date = datetime.datetime.strptime(d, "%Y-%m-%d").date()
    else:
        date = datetime.date.today()
    context['activePage'] = {'tree': 'ChannelPage', 'branch': 'index'}
    datas = get_datas_hourly(True, self.object.id, date)
    matches_data = get_datas_hourly(False,self.object.id, date)
    context['date'] = date
    context['labels'] = datas.keys()
    context['values'] = datas.values()
    context['matches_labels'] = matches_data.keys()
    context['matches_values'] = matches_data.values()
    context['executionLogs'] = ExecutionLog.objects.all()
    #^^^^^^^^^^^ You can access the execution logs in your template.
    return context

You have to iterate through the queryset and access the attributes one by one in the template, like

{% for item in execution_log %} 
    {{ item.type }} 
    {{ item.title }}
    {{ item.description }}
    .....
{% endfor %}

Upvotes: 2

Related Questions