Answers for "django mptt queryset"

1

django templateview

from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('about/', TemplateView.as_view(template_name="about.html")),
]
Posted by: Guest on January-12-2020
1

django createmany

>>> Category.objects.all().count()
2
>>> Category.objects.bulk_create(
    [Category(name="God"),
     Category(name="Demi God"),
     Category(name="Mortal")]
)
[<Category: God>, <Category: Demi God>, <Category: Mortal>]
>>> Category.objects.all().count()
5
Posted by: Guest on March-11-2020
-2

django imagefield modelform

@login_required
def Product_Adder_View(request):
    
    if request.method == 'POST':
        Prdct_form = ProductForm(request.POST, request.FILES)
        if Prdct_form.is_valid():
            Prdct_form.save()
            return HttpResponseRedirect(reverse('basic_app:Product_added'))
        else:
            raise('Not Valid Form')
    else:
        Prdct_form = ProductForm()
    return render(request, 'basic_app/product_adding.html', context={'Prdct_form':Prdct_form})
  
  
  # in form ModelForm
  class ProductForm(forms.ModelForm):
    # category = forms.ModelChoiceField(queryset= PCategory.order_by('cat_name'))
    category = forms.ModelChoiceField(queryset= PCategory.objects.all())
    product_image = forms.ImageField()
    class Meta:
        model = Products
        fields = ("category", "product_name", "product_image")
Posted by: Guest on April-05-2020

Python Answers by Framework

Browse Popular Code Answers by Language