如何使用视图向数据库创建数据

时间:2019-03-17 20:39:15

标签: django-models

我正在尝试从类似于购物车的目录中创建订单金额,但是对于所有已完成的订单,返回的金额均为1:

views.py

   def get_user_pending_order(request):
        #get order from correct profile
        user_profile = get_object_or_404(Profile,user=request.user)
        order = Order.objects.filter(owner=user_profile,is_ordered=True)
        if order.exists():
    #to get an order in the list of filtered orders
    return order[0]
    return 0

    def add_to_catalogue(request,employee_id):#product_id,employee_id
          user_profile= get_object_or_404(Profile, user =request.user)
          order_to_purchase = get_user_pending_order(request)
          amount= self.order_to_purchase.get_catalogue_total(),
          employee = Employee.objects.get(pk=employee_id)
          if employee in request.user.profile.ebooks.all():
               messages.info(request,'you already own this ebook')
          return redirect(reverse('freelance:employee_list'))
        order_task,status = 
        OrderTask.objects.get_or_create(employee=employee)
        user_order,status = Order.objects.get_or_create(owner=user_profile, 
 is_ordered=False,order_amount=amount)####TThis IS WHWERE TO EDIT TO PREVENT 
 RE ORDERNG OF FREELANCING
 user_order.tasks.add(order_task)
  if status:
    user_order.ref_code = generate_order_id()
    user_order.save()
messages.info(request,"task added to catalogue")
return redirect(reverse('freelance:employee_list'))

def get_user_pending_order(request):
       #get order from correct profile
       user_profile = get_object_or_404(Profile,user=request.user)
       order = Order.objects.filter(owner=user_profile,is_ordered=True)
       if order.exists():
       #to get an order in the list of filtered orders
      return order[0]
      return 0

models.py

    class Order(models.Model):
         ref_code = models.CharField(max_length=15)
         owner = models.ForeignKey(Profile, on_delete=models.SET_NULL, null= 
          True)
         is_ordered = models.BooleanField(default=False)
        tasks = models.ManyToManyField(OrderTask)
        date_ordered = models.DateTimeField(auto_now= True)
       order_amount =   models.DecimalField(default=0.01,  max_digits= 10, 
       decimal_places=2)

def order_tasks(self):
    return ','.join([str(c.employee) for c in self.tasks.all()])
def get_catalogue_tasks(self):
    return self.tasks.all()

def get_catalogue_total(self):
    return sum([task.employee.pricing for task in self.tasks.all()])
def __str__(self):
    return '{0} - {1}'.format(self.owner, self.ref_code, self.order_amount)


def tasks_summary(request):
            existing_order = get_user_pending_order(request)
            my_user_profile = Profile.objects.filter(user= 
            request.user).first()
            my_orders = Order.objects.filter(is_ordered= True, owner= 
            my_user_profile)
            order_to_purchase = get_user_pending_order(request)

            amount= order_to_purchase.get_catalogue_total(),
            order = Order.objects.filter(is_ordered= True)
        context = {
           'my_orders':my_orders,
           'order':order,
           'amount':amount
       #  'total':total,

         }

        return render(request, 
       'freelance/tasks_summary.html',context)###Belongs to the admin sisde

Output of the template

尝试在目录中添加任何内容时出现此错误:

    AttributeError at /admin/tasks_summary/
   'int' object has no attribute 'get_catalogue_total'

0 个答案:

没有答案