How do I override the get_context_data method of my DetailView so that it displays both the ticket details and its comment list? How do I get the comment object from a different app so that I can put it into the view? The end goal is to have both the Ticket Details and have the comment list on the same page. Here's what I have so far
This is my TicketDetailView
class TicketDetailView(DetailView):
model = Ticket
context_object_name = 'comments'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['comments'] = context['comments'].filter(author=self.request.user)
return context
This is my CommentListView
class CommentListView(ListView):
model = Comment
template_name = 'tickets/ticket_detail.html'
context_object_name = 'comments'
ordering = ['-date_posted']
paginate_by = 5
def get_queryset(self, *args, **kwargs):
ticket = self.kwargs['ticket']
return Comment.objects.filter(ticket=ticket).order_by(self.ordering)
Is there a way to return a dictionary through the get_queryset function of a class based view in Django? I want to pass the array tickets and the string email to my template, but I am only able to pass tickets right now.
Content of views.py:
class UserTicketListView(ListView):
model = Ticket
template_name = 'ticket_system/user_tickets.html'
context_object_name = 'tickets'
ordering = ['-date_posted']
paginate_by = 5
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
email = User.objects.get(username=user).email
return Ticket.objects.filter(author=user).order_by('-date_posted')
class UserTicketListView(ListView):
model = Ticket
template_name = 'ticket_system/user_tickets.html'
context_object_name = 'tickets'
ordering = ['-date_posted']
paginate_by = 5
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
queryset = self.get_queryset()
user = get_object_or_404(User, username=self.kwargs.get('username'))
email = User.objects.get(username=user).email
queryset = queryset.filter(author=user).order_by('-date_posted')
context['user'] = user
context['email'] = email
return context
What am i doing wrong here. I have been trying to figure this out for hours. I think i am having issues with Django get_context_data function.
Error is 'PatientBedAllotmentList' object has no attribute 'object'
views.py
#method_decorator(login_required, name='dispatch')
class PatientBedAllotmentList(ListView):
model = BedAllotment
template_name = 'room/bed_allotment_list.html'
def get_context_data(self, **kwargs):
context = super(PatientBedAllotmentList, self).get_context_data(**kwargs)
start = BedAllotment.objects.get(id=self.object.id).allotment_date
end = BedAllotment.objects.get(id=self.object.id).departure_date
amount = BedCreate.objects.get(id=self.object.id).cost
days_number = abs((end - start).days)
days_number = int(days_number)
amount_due = amount * days_number
context['account_type'] = AccountUser.objects.get(user_id=self.request.user.id)
hospital_id = AccountUser.objects.get(user_id=self.request.user.id).hospital_id
allotment_details = BedAllotment.objects.filter(hospital_id=hospital_id)
context['allotment'] = allotment_details
context['amount'] = amount_due
return context
urls.py
from django.conf.urls import url
from medisaver.room import views
urlpatterns = [
url(r'^room-category/create/$', views.RoomCategoryCreate.as_view(), name='room_category_create'),
url(r'^room-category/list/$', views.RoomCategoryList.as_view(), name='room_category_list'),
url(r'^room-category/update/(?P<hospital_id>[0-9A-Fa-f-]+)/(?P<category_id>[0-9]+)/$', views.RoomCategoryUpdate.as_view(), name='room_category_update'),
url(r'^room-category/delete/(?P<hospital_id>[0-9A-Fa-f-]+)/(?P<category_id>[0-9]+)/$', views.RoomCategoryDelete.as_view(), name='room_category_delete'),
url(r'^hospital-rooms/list/$', views.RoomList.as_view(), name='room_list'),
url(r'^hospital-rooms/create/$', views.RoomCreateView.as_view(), name='room_create'),
url(r'^hospital-rooms/update/(?P<category_id>[0-9]+)/(?P<room_id>[0-9]+)/$', views.RoomUpdate.as_view(), name='room_update'),
url(r'^hospital-rooms/delete/(?P<category_id>[0-9]+)/(?P<room_id>[0-9]+)/$', views.RoomDelete.as_view(), name='room_delete'),
url(r'^hospital-rooms/beds/create/$', views.BedCreateView.as_view(), name='bed_create'),
url(r'^hospital-rooms/beds/list/$', views.BedList.as_view(), name='bed_list'),
url(r'^hospital-rooms/beds/update/(?P<room_id>[0-9]+)/(?P<bed_id>[0-9]+)/$', views.BedUpdate.as_view(), name='bed_update'),
url(r'^hospital-rooms/beds/delete/(?P<room_id>[0-9]+)/(?P<bed_id>[0-9]+)/$', views.BedDelete.as_view(), name='bed_delete'),
url(r'^hospital-rooms/beds/patient-bed-allotment/$', views.BedAllotmentCreate.as_view(), name='bed_allotment'),
url(r'^hospital/discharge-patient/(?P<allotment_id>[0-9]+)/(?P<patient_id>[0-9A-Fa-f-]+)/$', views.BedDischarge.as_view(), name='patient_bed_discharge'),
url(r'^hospital/bed-allotment-list/$', views.PatientBedAllotmentList.as_view(), name='patient_bed_list'),
]
self.object does not make any sense in ListView.
If you want to get stay_time then you can do it in your model and access in template using object.stay_time and even you can calculate the amount in template by multiplying. But I You can even do it in detailview. Create a method in that model like
#property
def stay_time(self):
return (self.departure_date-self.allotment_date)
I was able to fix it by calling on form_valid function. I used the function on my BedDischarge class in my views. And was able to use self.object. Then made a template call in my templates.
#method_decorator(login_required, name='dispatch')
class BedDischarge(UpdateView):
model = BedAllotment
template_name = 'room/bed_discharge.html'
form_class = BedAllotmentUpdateForm
pk_url_kwarg = 'allotment_id'
success_url = reverse_lazy('patient_bed_list')
def get_context_data(self, **kwargs):
context = super(BedDischarge, self).get_context_data(**kwargs)
context['account_type'] = AccountUser.objects.get(user_id=self.request.user.id)
return context
def form_valid(self, form):
get_allot_id = self.object.id
bed_allot = BedAllotment.objects.get(id=get_allot_id)
start_time = bed_allot.allotment_date
end_time = form.instance.departure_date
cost = BedCreate.objects.get(id=self.object.bed_id).cost
days_spent = (end_time - start_time).days
amount = cost * days_spent
form.instance.days = days_spent
form.instance.amount = amount
return super(BedDischarge, self).form_valid(form)
I have a problem how to pass selected data from SelectDateWidget to view and to url.
Now I have the hardcoded data get_success_url in the digest, after the "submit" on the form.
For the SelectDateWidget help, I want to select the month and year for which I want to show selected events.
Here is my form:
class BookingForm(forms.ModelForm):
date_start = forms.DateField(widget=SelectDateWidget(years=range(1980, 2018)))
class Meta:
model = Booking
fields = ('date_start', )
widgets = {'date_start': SelectDateWidget()}
This is my view, where I have hardcoded value for success url:
class BookingListView(ListView, FormView):
model = models.Booking
form_class = BookingForm
queryset = models.Booking.objects.all() # order_by('-date_start')
paginate_by = 80
template_name = 'events/archive_list.html'
context_object_name = 'object_list'
date_field = 'date_start'
allow_future = True
def get_context_data(self, **kwargs):
context = super(BookingListView, self).get_context_data(**kwargs)
context['mode'] = 'archive'
context['form'] = BookingForm()
return context
def get_success_url(self):
return reverse('archive:archive_month_numeric', kwargs={'year': 2014, 'month': 10})
My url to page where I have events in selected date:
url(r'^/(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$', views.ArticleMonthArchiveView.as_view(month_format='%m'), name="archive_month_numeric"),
Why get_queryset() from MonthArchiveView returns all objects from my model instead objects created only in requested month?
class BudgetMonthlyView(MonthArchiveView):
template_name = 'budget/monthly.html'
model = FinanceData
date_field = "created"
make_object_list = False
allow_future = False
month_format = '%m'
def get_context_data(self, **kwargs):
context = super(BudgetMonthlyView, self).get_context_data(**kwargs)
print(self.get_queryset()) #return all objects from FinanceData model
print(context['object_list']) #works fine
return context
It's just the way that the MonthArchiveView is implemented. If you look at the source code, you can see that the object_list is returned by the get_dated_items method.
It's probably implemented this way because the date archive views add other things to the context as well as the object_list, for example date_list.
class BudgetMonthlyView(MonthArchiveView):
template_name = 'budget/monthly.html'
queryset = FinanceData.objects.all()
date_field = "created"
make_object_list = True
allow_future = False
month_format = '%m'
paginate_by = 50
context_object_name = 'object_list'
def get_context_data(self, **kwargs):
context = super(BudgetMonthlyView, self).get_context_data(**kwargs)
month = self.get_month() # get month
context['month'] = self.queryset.filter(created__month=month) # you can aggregate for this month ' .aggregate(Sum('cost'))['cost__sum'] '
print(context['object_list']) #works fine
return context