Как взять массив внешних ключей в модели Django - PullRequest
0 голосов
/ 28 марта 2019

У меня есть модель товара, цена и заказчик. Я хочу список продуктов при создании заказа, который используется в качестве внешнего ключа в custorder. Я использую Postgres в качестве базы данных.

class Product(models.Model):

    product = ArrayField(models.CharField(max_length=200), blank=True)

    def __str__(self):

         return str(self.product)

class Price(models.Model):

    product = models.ForeignKey(Product, on_delete=models.CASCADE)

    price = models.DecimalField(max_digits=50, decimal_places = 5, default=0)

    def __str__(self):

        return "%s" % self.price

class CustOrder(models.Model):

    Customer_id = models.AutoField(primary_key=True)
    CustomerName = models.CharField(max_length=200)
    email = models.EmailField(max_length=70,blank=True, null= True, unique= True)
    gender = models.CharField(max_length=6, choices=GENDER_CHOICES)
    phone = PhoneField(null=False, blank=True, unique=True)
    landmark = models.PointField()
    #landmark = models.TextField(max_length=400, help_text="Enter the landmark", default='Enter landmark')
    houseno = models.IntegerField(default=0)
    #product_name = models.CharField(max_length=200, choices=PRODUCT_CHOICES,default='Boneless chicken')
    product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True,related_name='pricetag')
    quantity = models.IntegerField(default=0)
    price = models.ForeignKey(Price, on_delete=models.SET_NULL, null=True,related_name='pricetag')
    #price = models.DecimalField(max_digits=50, decimal_places=5, default=48.9)
    pay_method = models.CharField(max_length=200,choices=PAYMENT_CHOICES, default='RAZOR PAY')
    city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True) 
    area = models.ForeignKey(Area, on_delete=models.SET_NULL, null=True)
    Price.objects.aggregate(Sum('price'))
    def __str__(self):
         return self.CustomerName
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...