1

我有两个模型OrderOrderitem Order收集客户信息并跟踪客户订单的状态。

class Order(SmartModel):
    #each individual status
    SUBMITTED = 1 # the credit card was valid or mobilemoney was recieved.It is ready for us to process the order
    PROCESSED = 2 # After submitted orders are reviewed, we can mark them as processed, letting deliverers know order is ready to be shipped
    DELIVERED = 3 # the order has been processed and approved by the adminstrator(in this case us), it can delivered.
    CANCELLED = 4 # Customer called the company and decided they didnt want to go through with the order either by phone or email.

    # SET OF POSSIBLE STATUSES
    ORDER_STATUSES = ((SUBMITTED,'Submitted'),(PROCESSED,'Processed'),(DELIVERED,'Delivered'),(CANCELLED,'Cancelled'),)
    #Order info
    date = models.DateTimeField(auto_now_add=True)
    status = models.IntegerField(choices=ORDER_STATUSES, default=SUBMITTED)
    last_updated = models.DateTimeField(auto_now=True)
#   transaction_id = models.CharField(max_length=20)

    #contact info
    email = models.EmailField(max_length=50)
    phone = PhoneNumberField(max_length=20,default='+250785559899')

    #delivery information
    delivery_name = models.CharField(max_length=50)
    delivery_city = models.CharField(max_length = 50)
    delivery_address = models.CharField(max_length=50,help_text="e.g Klab 6th floor,Telecomhouse,Kacyiru")
    restaurant = models.ForeignKey(Restaurant,default='1')

OrderItem另一方面持有物品和数量

class OrderItem(SmartModel):
    shopping_id = models.CharField(max_length=50,db_index=True)
    quantity = models.IntegerField(default=0)
    item = models.ForeignKey(Item)
    order = models.ForeignKey(Order)

我正在创建这样的模型。

try:
   order= Order.objects.get(pk=order_id)
   restaurant = Restaurant.objects.get(pk=restaurant_id)
except:
   order = Order(created_by=anon_user,modified_by=anon_user)
   order.save()
   oi=OrderItem(order=order,shopping_id=_shopping_id(request),
                                          quantity=quantity,
                                          item=i,
                                          created_by=anon_user,
                                          modified_by=anon_user)
   oi.save()

现在,您看到OrderItem我包含了一个外键order,这是因为我希望能够拥有一个模型,其中包含一个订单项目、它的数量和Order详细信息。但这不起作用我得到一个无类型错误,coercing to Unicode: need string or buffer, NoneType found.

我需要帮助来了解我如何才能完成这项工作。

Traceback:
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  136.                     response = response.render()
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/response.py" in render
  104.             self._set_content(self.rendered_content)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  81.         content = template.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  62.     return self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  185.                         nodelist.append(node.render(context))
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  1202.                     return self.nodelist.render(new_context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  476.         output = self.nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  281.                 return nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  281.                 return nodelist.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/template/debug.py" in render
  87.             output = force_unicode(output)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_unicode
  71.                 s = unicode(s)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/forms.py" in __unicode__
  411.         return self.as_widget()
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/forms.py" in as_widget
  458.         return widget.render(name, self.value(), attrs=attrs)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/widgets.py" in render
  547.         options = self.render_options(choices, [value])
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/widgets.py" in render_options
  570.         for option_value, option_label in chain(self.choices, choices):
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/models.py" in __iter__
  897.                 yield self.choice(obj)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/models.py" in choice
  903.         return (self.field.prepare_value(obj), self.field.label_from_instance(obj))
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/forms/models.py" in label_from_instance
  954.         return smart_unicode(obj)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in smart_unicode
  39.     return force_unicode(s, encoding, strings_only, errors)
File "/home/mats-invasion/projects/f4l/env/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_unicode
  71.                 s = unicode(s)

Exception Type: TypeError at /incoming_orders/orderitem/create/
Exception Value: coercing to Unicode: need string or buffer, NoneType found
4

1 回答 1

0

首先,您应该始终调试代码并找到出现错误的地方。

我认为有些问题出在这几行:

order = Order(created_by=anon_user,modified_by=anon_user)
order.save()

因为您尝试在没有所有必填字段(例如电子邮件字段)的情况下创建和保存模型实例

于 2013-02-21T08:23:43.677 回答