我有一个模型Order
,
class Order(SmartModel):
date = models.DateTimeField(...)
status = models.IntegerField(...)
restaurant = models.ForeignKey(Restaurant,null=True,blank=True,default = None,help_text="The restaurant the customer order from")
email = models.EmailField(...)
mobile = PhoneNumberField(...)
我正在像这样保存对象,
orders = Order()
checkout_form = forms.CheckoutForm(request.POST,instance=orders)
orders = checkout_form.save(commit=False)
anon_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
orders.created_by = anon_user
orders.modified_by = anon_user
orders.status = Order.SUBMITTED
orders.save()
如您所见,我没有保存餐厅(fk 字段),我应该怎么做。我看过这个(https://groups.google.com/forum/?fromgroups=#!topic/django-users/PcSDKZhPVmc)和其他几个例子,但它们让我更加困惑。
我需要绑定 ~Restaurant~ 来点菜,这样当我查找订单时,我就知道它属于哪家餐厅。
烦人的事情是我无法加载 djangoproject.com,已经一个星期了。
表格.py,
class CheckoutForm(forms.ModelForm):
class Meta:
model = Order
exclude = ('created_by','modified_by','is_active','status','restaurant','date')
模板,
<table>
{{ form.errors }}
{{ form.non_field_errors }}
{% form_table_row form.email %}
{% form_table_row form.mobile %}
</table>