One To Many and models

时光毁灭记忆、已成空白 提交于 2020-04-18 05:44:09

问题


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.

so I have User>>Order>>Multi Items. questions:

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)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!


回答1:


    if form.is_valid():
        order = get_object_or_404(Order, id=id)
        instance = form.save(commit=False)
        instance.order=order
        instance.save()

just need to do: commit=False and then link the order.



来源:https://stackoverflow.com/questions/60603216/one-to-many-and-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!