一对多和模型 [英] One To Many and models
问题描述
我正在尝试建立我的第一个项目,一个CRM网站来处理订单和库存。
而我陷入困境,我能够将订单链接到客户。
,但是当我尝试建立包含多个项目的订单时。由于某种原因,我找不到解决方法。
希望您能为我提供帮助。
i am trying to build my first project, a CRM website to handle orders and inventory. and i got stuck, i was able to link orders to customer. but when i try to build order that contain multi items. for some reason i didn't find a way to do it. hope you can assist me.
所以我有User >> Order >> Multi Items。
问题:
so I have User>>Order>>Multi Items. questions:
1)此处的最佳做法是仅使用ForeignKey吗?
这是我的模型代码:
1) does the best practice here is just use ForeignKey ? this my model's code:
from django.db import models
class Customer(models.Model):
name = models.CharField(max_length=200, null=True)
phone = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
def date_createdview(self):
return self.date_created.strftime('%B %d %Y')
class Product(models.Model):
CATEGORY = (
('General', 'General'),
('SmartHome', 'SmartHome'),
('Software', 'Software'),
('Mobile', 'Mobile'),
)
name = models.CharField(verbose_name='שם', max_length=200, null=True)
price = models.FloatField(verbose_name='מחיר', null=True)
category = models.CharField(max_length=200, null=True, choices=CATEGORY, verbose_name='קטגוריה')
description = models.CharField(max_length=200, null=True, verbose_name='מלל חופשי')
date_created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
class Order(models.Model):
STATUS = (
('New', 'New'),
('Work in progress', 'Work in progress'),
('completed', 'completed'),
)
customer = models.ForeignKey(Customer, null=True, on_delete=models.CASCADE)
date_created = models.DateTimeField(auto_now_add=True, null=True)
status = models.CharField(max_length=200, null=True, choices=STATUS)
def date_createdview(self):
return self.date_created.strftime('%d/%m/%Y')
class OrderItem(models.Model):
product = models.ForeignKey(Product, null=True, on_delete=models.CASCADE)
order = models.ForeignKey(Order, null=True, on_delete=models.CASCADE)
quantity = models.IntegerField(null=True)
2)如何我应该建立自己的观点或形式吗?
我想使其动态化,当我输入订单时,我可以插入项目并看到新项目被添加到订单中的项目列表中。
如何保存订单号并添加新商品?
2)how should I build my views or forms? i want to make it dynamic, when i enter the order i can insert items and see the new item get add to a list of the items in the order. how can save the order number and add new items?
这是我的产品视图,正在运行。我可以添加新产品。
this is my product view, it's working. I can add new products.
@login_required(login_url="login")
def products(request):
form = ProductForm(request.POST or None)
if form.is_valid():
form.save()
products = Product.objects.all()
context = {'form': form, 'products': products}
return render(request, 'accounts/products.html', context)
希望您可以将我定向到正确的地方。
谢谢!
hope you can direct me to the right place. thank you!
推荐答案
if form.is_valid():
order = get_object_or_404(Order, id=id)
instance = form.save(commit=False)
instance.order=order
instance.save()
只需要这样做:
commit = False
然后链接订单。
just need to do: commit=False and then link the order.
这篇关于一对多和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!