Home > Net >  AttributeError: 'str' object has no attribute 'days'
AttributeError: 'str' object has no attribute 'days'

Time:04-29

when I try to add an item to a vendor it calls the POST method when this *view is called it shows an error that 'str' object has no attribute 'days'. if you want any other files to tell I hope someone will fix this issue.

class AddBaseproductToStore(AdminOnlyMixin, generic.TemplateView):
template_name = 'vendor-admin/add-product-to-store.html'

def get_context_data(self, **kwargs):
    # Call the base implementation first to get a context
    context = super().get_context_data(**kwargs)
    context['prod_id'] = self.kwargs['pk']
    context['vendor'] = self.kwargs['vendor']
    return context

def get(self, request, *args, **kwargs):
    product = BaseProduct.objects.get(id=self.kwargs['pk'])
    vendor_id = self.kwargs['vendor']
    base_cat = BaseCategory.objects.all()
    return render(request, self.template_name, {"product": product, "base_cat": base_cat, 'vendor': vendor_id})

this the continuation of above code

    def post(self, request, *args, **kwargs):
    base_product = BaseProduct.objects.get(id=self.kwargs['pk'])
    vendor_id = self.kwargs['vendor']
    base_category = BaseCategory.objects.get(id=request.POST.get('category'))
    try:
        p = Product.objects.get(base_product=base_product,
                                category=Category.objects.get(vendor_id=vendor_id, base_category=base_category))
    except ObjectDoesNotExist:
        product = Product()
        product.category = Category.objects.get(vendor_id=vendor_id, base_category=base_category)
        product.base_product = base_product
        product.name = request.POST.get('name')
        product.ar_name = request.POST.get('ar_name')
        product.sort_order = request.POST.get('sort-order')

        product.dryclean = request.POST.get('dryclean', '') == 'on'
        product.normal_dryclean_price = request.POST.get('dryclean_price')
        product.normal_dryclean_buffer_time = request.POST.get('dryclean_buffer')

        product.wash_and_pressing = request.POST.get('wash-press', '') == 'on'
        product.normal_wash_and_pressing_price = request.POST.get('wash-press-price')
        product.normal_wash_and_pressing_buffer_time = request.POST.get('wash-press-buffer')

        product.pressing = request.POST.get('press', '') == 'on'
        product.normal_pressing_price = request.POST.get('press-price')
        product.normal_pressing_buffer_time = request.POST.get('press-buffer')

        product.express_dryclean = request.POST.get('exp-dryclean', '') == 'on'
        product.express_dryclean_price = request.POST.get('exp-dryclean-price')
        product.express_dryclean_buffer_time = request.POST.get('exp-dryclean-buffer')

        product.express_wash_and_pressing = request.POST.get('exp-wash-press', '') == 'on'
        product.express_wash_and_pressing_price = request.POST.get('exp-wash-press-price')
        product.express_wash_and_pressing_buffer_time = request.POST.get('exp-wash-press-buffer')

        product.express_pressing = request.POST.get('exp-press', '') == 'on'
        product.express_pressing_price = request.POST.get('exp-press-price')
        product.express_pressing_buffer_time = request.POST.get('exp-press-buffer')

        product.save()
        if product.express_pressing == True or product.express_dryclean == True or product.express_wash_and_pressing_price == True:
            product.express = True
        else:
            product.express = False
        product.save()

    return redirect('admins:vendor_items', pk=vendor_id)

this is the error showing

\lib\site-packages\django\utils\duration.py", line 44, in duration_microseconds
    return (24 * 60 * 60 * delta.days   delta.seconds) * 1000000   delta.microseconds
AttributeError: 'str' object has no attribute 'days'

CodePudding user response:

convert all your times field to timedelta by parse_duration() method

eg:

from django.utils.dateparse import parse_duration
product.normal_dryclean_buffer_time = parse_duation(request.POST.get('dryclean_buffer'))

do it for all fields.

  • Related