Django自定义表创建

时间:2018-10-02 15:27:10

标签: django django-models

我有一个项目,我必须在其中创建一个命令表,以便客户在站点上进行命令。我试图实现的目标是为客户提供选择多种产品的可能性。对于一个产品,外键是可以的,但是当我们必须选择多个产品时,则不可以。即使单击“ +”添加按钮,也不允许添加更多,但是django使您可以在DB:D中添加新产品。

我如何实现将多个产品添加到购物车的目标? enter image description here

提前谢谢!

我的模特;

from django.db import models
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
from datetime import datetime, timedelta, time
today = datetime.now().date()


class ClientManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ClientManager, self).filter(timestamp__lte=datetime.now())


class Client(models.Model):
    TYPE_CLIENT = (
        ('PF', 'Persoana Fizica'), ('PJ', 'Persoana Juridica'),)
    MODEL_CLIENT = (
        ('O', 'On-line'), ('S', 'Showroom'), ('D', 'Distribuitor'))

    user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True, default=1, on_delete=True)
    model_client = models.CharField(max_length=3, blank=True, default="PF", null=True,
                                    choices=MODEL_CLIENT, help_text='Selecteaza de unde vine clientul')

    tip_client = models.CharField(max_length=3, blank=True, default="PF", null=True,
                                   choices=TYPE_CLIENT, help_text='Selecteaza tipul de client')
    nume_client = models.CharField(max_length=30, blank=True, default="", null=True,
                                     help_text='Insereaza Numele si Prenumele Clientului')
    adresa_client = models.CharField(max_length=50, blank=True, default="", null=True,help_text='Insereaza Adresa Clientului')
    cnp_client = models.CharField(
        max_length=12, blank=True, default="", null=True, help_text='Insereaza CNP-ul Clientului')
    serie_numar_client = models.CharField(max_length=20, blank=True, default="", null=True,help_text='Insereaza Seria si Numarul Clientului')
    email = models.EmailField(blank=True, null=True)
    nume_firma = models.CharField(max_length=30, blank=True, default="",null=True, help_text='Insereaza Numele Firmei')
    cui_firma=models.CharField(max_length=10, blank=True, default="",null=True, help_text='Insereaza CUI-ul Firmei')
    adresa_firma = models.CharField(
        max_length=70, blank=True, default="", null=True, help_text='Insereaza Adresa Firmei')


    updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True, null=True)

    objects = ClientManager()

    def __str__(self):
        return str(self.nume_client) + " | " + str(self.adresa_client) + " | " + str(self.adresa_firma) + " | " + str(self.nume_firma) + " | "  + str(self.timestamp)

    class Meta:
        verbose_name_plural = "client"
        ordering = ["-timestamp"]


class ProdusManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ProdusManager, self).filter(timestamp__lte=datetime.now())

class Produs(models.Model):


    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             blank=True, null=True, default=1, on_delete=True)
    nume_produs = models.CharField(max_length=30, blank=True, null=True,
                                help_text='Adauga Nume Produs')

    pret_produs = models.IntegerField(blank=True,  null=True,help_text='Introdu Pretul Produsului')
    timestamp = models.DateTimeField(
        auto_now=False, auto_now_add=True, blank=True, null=True)
    objects = ProdusManager()

    def __str__(self):
        return str(self.nume_produs) + " | " + str(self.pret_produs) 

    class Meta:
        verbose_name_plural = "produs"
        ordering = ["-timestamp"]


class ComandaManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ComandaManager, self).filter(timestamp__lte=datetime.now())


class Comanda(models.Model):
    STATUS_PRODUS = (
        ('S', 'Stoc'), ('SD', 'Stoc Distribuitor'), ('L', 'Livrat'))
    user = models.ForeignKey(settings.AUTH_USER_MODEL,
                             blank=True, null=True, default=1, on_delete=True)
    nume_client_comanda = models.ForeignKey(
        Client, related_name='nume_client_comanda', on_delete=models.CASCADE)
    nume_firma_comanda = models.ForeignKey(
        Client, related_name='nume_firma_comanda', on_delete=models.CASCADE)
    produsul_comandat = models.ForeignKey(
        Produs, related_name='produsul_comandat', on_delete=models.CASCADE)
    pretul_comandat = models.ForeignKey(
        Produs, related_name='pretul_comandat', on_delete=models.CASCADE)
    cantitate = models.IntegerField (blank = True,  null = True, help_text = 'Introdu Cantitatea Produsului')
    status_produs = models.CharField(max_length=3, blank=True, default="PF", null=True,
                                     choices=STATUS_PRODUS, help_text='Selecteaza de unde vine clientul')
    pret_comanda = models.IntegerField(
        blank=True,  null=True, help_text='Introdu Pretul Comenzii')
    timestamp = models.DateTimeField(
        auto_now=False, auto_now_add=True, blank=True, null=True)
    objects = ComandaManager()

    def __str__(self):
        return str(self.nume_client_comanda) + " | " + str(self.pret_comanda)
    #    def __str__(self):
    #     return "%s %s" % (self.first_name, self.last_name)
    class Meta:
        verbose_name_plural = "comanda"
        ordering = ["-timestamp"]

1 个答案:

答案 0 :(得分:0)

您需要一种Many to Many关系,而不是ForeignKey(一对多)

这样,多个客户可以各自拥有多个产品。

现在,两个不同的客户端不能拥有相同的产品,因为您使用的是ForeignKey。