1

私は2つのモデルOrderを持っておりOrderitem 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しかし、これは機能していません.noneタイプのエラーが発生します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 に答える