auto-populate Model-B with Model-A and/or Model-C

15 views
Skip to first unread message

user-django

unread,
Apr 27, 2020, 8:41:34 PM4/27/20
to Django users
firstly abstractly speaking - each time i create an order via Model A i want to auto-populate Model B with the data from Model-A and/or Model-C.

now straight to the point - i want both, the customer's name & the product (which they are purchasing) to automatically appear as a concatenated string in the third model, a different one.

class Customer name = models.CharField(max_length=200, null=True)
plus
class Product name = models.CharField(max_length=200, null=True)
equals to
class Account... customer + product

how to achieve this ?


here are my main files.

models.py
from django.db import models

# Create your models here.

class CustomerKlase(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)
    dateCreated = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return '{}'.format(self.name)


class TagKlase(models.Model):
    name = models.CharField(max_length=200, null=True)
    def __str__(self):
        return '{},'.format(self.name)


class ProductKlase(models.Model):
    THE_CATEGORY = (
        ("Indoor0","Indoor1"),
        ("Outdoor0","Outdoor1")
        )
    name = models.CharField(max_length=200, null=True)
    price = models.FloatField(null=True)
    category = models.CharField(max_length=200, null=True, choices=THE_CATEGORY, blank=True)
    description = models.CharField(max_length=200, null=True, blank=True)
    dateCreated = models.DateTimeField(auto_now_add=True)
    tags = models.ManyToManyField(TagKlase)
    def __str__(self):
        return '{}'.format(self.name)


class AccountKlase(models.Model):
    customer = models.ForeignKey(CustomerKlase, null=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(ProductKlase, null=True, on_delete=models.SET_NULL)
    def __str__(self):
        return '{} & {}'.format(self.customer, self.product)


class OrderKlase(models.Model):
    THE_STATUS = (
        ("Pending-0","Pending-1"),
        ("Out for delivery 0","Out for delivery 1"),
        ("delivered-0","delivered-1")
        )
    customer = models.ForeignKey(CustomerKlase, null=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(ProductKlase, null=True, on_delete=models.SET_NULL)
    account = models.ForeignKey(AccountKlase, null=True, on_delete=models.SET_NULL)
    dateCreated = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=200, null=True, choices=THE_STATUS)
    def __str__(self):
        return '{}'.format(self.account)

views.py
from django.shortcuts import render

# Create your views here.

from .models import *

def homepageFuncte(request):
    return render(request, "myapp1/my-templates/homepage.html")

def productsFuncte(request):
    products = ProductKlase.objects.all()
    context = {"my_Products":products}
    return render(request, "myapp1/my-templates/products.html", context)

def customersFuncte(request):
    customers = CustomerKlase.objects.all()
    context = {"my_Customers":customers}
    return render(request, "myapp1/my-templates/customers.html", context)

def ordersFuncte(request):
    orders = OrderKlase.objects.all()
    #customers = orders.customerklase_set.all()[0]#"customers":customers, 
    context = {"my_Orders":orders}
    return render(request, "myapp1/my-templates/orders.html", context)
Reply all
Reply to author
Forward
0 new messages