Thursday, January 2, 2020

How to create "form" from "model" with 2 foreign keys?

'''
REFERENCE: https://stackoverflow.com/questions/53091688/modeling-product-orders-in-django
'''


from django.db import models
from django.utils import timezone
from django.core.validators import RegexValidator


import datetime


# Create your models here.
class Customer(models.Model):
name = models.CharField(max_length=50)

phone_regex = RegexValidator(regex='^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=False)

email = models.EmailField(max_length=254)
dob = models.DateField(blank=True)

def __str__(self):
return self.name


class Product(models.Model):
item_name = models.CharField(max_length=255)
price = models.PositiveIntegerField()

def __str__(self):
return self.item_name


class OrderDetail(models.Model):
order = models.ForeignKey(Customer, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.PositiveIntegerField(default=1)
order_date_time = models.DateTimeField()

def __str__(self):
return self.order.name
Context: I'm new to Django. I've just completed the tutorial on the Docs page. I'm trying to make a coffee ordering application. Basically a person has to enter his details and select coffee along with number of cups.
Now a person may select 2 or 3 of the same type of coffee, or he may select multiple coffee types.

I've attached my models.py file. How do i make a form for this mode?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/444aa5a1-5020-4970-8b7e-c5f2225a760c%40googlegroups.com.

No comments:

Post a Comment