Django - selectively query models using pk_url_kwarg - python

Consider the these two models and view:
models.py
class BHA_List(models.Model):
well = models.ForeignKey(WellInfo, 'CASCADE', related_name='bha_list')
bha_number = models.CharField(max_length=100)
class BHA_overall(models.Model):
bha_number = models.ForeignKey(BHA_List, 'CASCADE', related_name='bha_overall')
drill_str_name = models.CharField(max_length=111)
depth_in = models.CharField(max_length=111)
views.py
class BHA_UpdateView(UpdateView):
model = BHA_overall
pk_url_kwarg = 'pk_alt'
form_class = BHA_overall_Form
To my understanding, pk_url_kwarg = 'pk_alt' will query and return instances of model = BHA_overall.
Let's say that I use a different CBV other than UpdateView, and want to implement two models. So something like this:
model = (BHA_overall, BHA_List). Is there any way that I force my pk_url_kwarg = 'pk_alt' to query and return instances only in BHA_List, but force my get_object() return objects in BHA_overall?? What CBV should I use?

you can use just 'View' and define methods post and get.
about like this:
class SomeView(View):
model = BHA_List
template_name = 'some.html'
def get(request, **kwargs):
overall = BHA_overall.objects.all()
return render(request,
self.template_name,
locals())
def get(request, **kwargs):
return render(request, self.template_name, {})

Related

how to put several objects in one template

How do I pass the object from ClassDetail to CreateNewStudent in order to use it in its template ?
Thanks.
class ClassDetail(DetailView):
context_object_name = "Class"
template_name = "temp/students.html"
model = Class
class CreateNewStudent(CreateView):
model = Student
form_class = forms.StudentForm
template_name = "temp/newstudent.html"
As I see you have two models Class and Student. In this case better way to create custom view:
def myCustomView(request, pk):
get_class = get_object_or_404(Class, pk=pk)
student_form = StudentForm
if request.POST:
student_form = StudentForm(request.POST)
if student_form.is_valid():
...
return ...
else:
return render(request, 'index.html', {'get_class':get_class, 'student_form':sudent_form})
else:
return render(request, 'index.html', {'get_class':get_class, 'student_form':sudent_form})

Pass context data from generic.DetailView

How can i pass the context data which is coming from a forms.py to my views class which is using generic detailView, i need to pass forms.py to my product detail page.
Here is the code for my view class
class ProductView(generic.DetailView):
model = Product
cart_product_form = CartAddProductForm()
context = {'cart_product_form': cart_product_form}
template_name = 'shopping/product.html'
query_pk_and_slug = True
Please let me know if this is incorrect
Override get_context_data, and add the form to the context before returning it.
class ProductView(generic.DetailView):
model = Product
template_name = 'shopping/product.html'
query_pk_and_slug = True
def get_context_data(self, **kwargs):
context = super(ProductView, self).get_context_data(**kwargs)
cart_product_form = CartAddProductForm()
context['cart_product_form'] = cart_product_form
return context

How can I return regular response in override get method, Django

I try to use class based views in Django. And I have such problem: I define a base class for a blog (BlogBaseView) and two other classes, that inherit it.
And in the second class(BlogIndexView) I want to make the search by get request, so I have override get method. It works, but if I don't make get request, it returns HttpResponse, however I want to return usual context (which BlogIndexView retunes without override get method).
What can I do?
class BlogBaseView(View):
def get_context_data(self, **kwargs):
context = super(BlogBaseView, self).get_context_data(**kwargs)
blog_categories = []
categories = BlogCategory.objects.all()
for category in categories:
blog_categories.append(tuple([category, category.get_number_of_category_items]))
context['name_page'] = 'blog'
context['tags'] = Tag.objects.all()
context['blog_categories'] = blog_categories
return context
class BlogIndexView(BlogBaseView, ListView):
queryset = Post.objects.all().order_by('-date_create')
template_name = 'index_blog.html'
context_object_name = 'posts'
def get(self, request):
if request.GET.get('tag'):
context = {
'posts' : Post.objects.filter(tags__name__in=[request.GET.get('tag')])
}
return render(request, self.template_name, context)
return HttpResponse('result')
class BlogFullPostView(BlogBaseView, DetailView):
model = Post
template_name = 'full_post.html'
pk_url_kwarg = 'post_id'
context_object_name = 'post'
Thanks!
ListView class also has a get_context_data method, so you should override that instead of get method. Using super you'll get access to BlogBaseView.get_context_data and then you can extended the result.
Here's how:
class BlogIndexView(BlogBaseView, ListView):
queryset = Post.objects.all().order_by('-date_create')
template_name = 'index_blog.html'
context_object_name = 'posts'
def get_context_data(self, **kwargs):
# use the (super) force Luke
context = super(BlogIndexView, self).get_context_data(**kwargs)
if self.request.GET.get('tag'):
context['posts'] = Post.objects.filter(tags__name__in=[self.request.GET.get('tag')])
return context
If you are overriding ListView then it's not a good idea to override the get method, as you will lose a lot of the ListView functionality.
In this case, it would be a better idea to override get_queryset, and do the search there.
def get_queryset(self):
queryset = super(BlogIndexView, self). get_queryset()
if request.GET.get('tag'):
queryset = queryset.filter(tags__name=request.GET['tag'])
return queryset

How do I write Django related queries using DetailView

How do I write a DetailView in Django, that has other related Queries?
Here is my example:
class DistributionDetailView(DetailView):
model = Distribution
template_name = 'projects/view_distribution.html'
def get_context_data(self, **kwargs):
context = super(DistributionDetailView,
self).get_context_data(**kwargs)
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution_name=Distribution)
context['sales_data'] = SalesData.objects.filter(
distibution_name=Distribution)
context['theatrical_release'] = TheatricalRelease.objects.filter(
distibution_name=Distribution)
return context
Distribution is the model class, you can't use it inside filter(). In the get_context_data method, you can access the current object with self.object, for example:
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution_name=self.object)
You can access the object with self.object in most generic views:
class DistributionDetailView(DetailView):
model = Distribution
template_name = 'projects/view_distribution.html'
def get_context_data(self, **kwargs):
context = super(DistributionDetailView,
self).get_context_data(**kwargs)
context['weekly_attendance'] = WeeklyAttendance.objects.filter(
distibution=self.object)
context['sales_data'] = SalesData.objects.filter(
distibution=self.object)
context['theatrical_release'] = TheatricalRelease.objects.filter(
distibution=self.object)
return context

Combining filters in django

I am using django_filters and I have a little problem with combining it each other.
What do I have? Basic filters, for example:
class BasicFilter(django_filters.FilterSet):
class Meta:
model = myModel
fields = []
class TimeFilter(BasicFilter):
created = django_filters.DateFromToRangeFilter(
help_text='Date from - to', label='Time'
)
class Meta(BasicFilter.Meta):
fields = ['created']
class AgentFilter(BasicFilter):
agent = django_filters.ModelMultipleChoiceFilter(
queryset=AgentClass.objects.all(), help_text=''
)
class Meta(BasicFilter.Meta):
fields = ['agent']
class SomethingElseFilter(BasicFilter):
something = django_filters.ModelMultipleChoiceFilter(
queryset=SomethingElse.objects.all(), help_text=''
)
class Meta(BasicFilter.Meta):
fields = ['something']
The user will decide which filters he wants, e.g. He will choose TimeFilter and AgentFilter, and I need to connect this basic filters to one ConnectedFilter.
Then I handle it in my views, e.g.
class MyView(ListView):
model = myModel
template_name = "filters.html"
def get_queryset(self):
qs=MyModel.objects.all()
try:
self.connected_filter = ConnectedFilter(
self.request.GET, queryset = qs)
return self.connected_filter
except:
return qs
def get_context_data(self, **kwargs):
context = super(MyView, self).get_context_data(**kwargs)
context['filter_form'] = self.connected_form.as_table()
return context
Or maybe there's some better solution.

Categories