Assume that I am a furniture manufacturer. I have many customers and each of them buy from me many diffrent products. In Django I will write class for Customer, Product and the most important for me Order from customer.
How can I do something like filter for that?
class Customer(models.Model):
name = models.CharField(max_length=10, blank=False)
class Product(models.Model):
name = models.CharField(max_length=10, blank=False)
customer_number_product = models.SmallIntegerField(max_length=6, blank=False)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE())
class Order(models.Model):
order_number = models.IntegerField(max_length=4, blank= False)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE())
orderdate = datetime...
deliverydate = datetime...
ordered_product = HERE IS A PROBLEM
I need to choose from the Product database only one product belongs to specific customer. I do not want to choose from list with thousands products which I sell to all my customers.
These are just your definitions for your models. Your various views and forms are responsible for doing whatever filtering is required to build and render the desired page(s).
But how can I build an instance with these information and in this way, with drop down list with products for one customer? Could you give me any advices?
models.py
class Customer(models.Model):
name = models.CharField(max_length=10, blank=False)
class Product(models.Model):
name = models.CharField(max_length=10, blank=False)
customer_number_product = models.SmallIntegerField(max_length=6, blank=False)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE())
class Order(models.Model):
order_number = models.IntegerField(max_length=4, blank= False)
customer_name = models.ForeignKey(Customer, on_delete=models.CASCADE())
orderdate = datetime...
deliverydate = datetime...
ordered_product = models.OneToOneField(Product, on_delete=models.CASCADE())
views.py
def new_order(request):
form = OrderForm(request.POST or None)
what sould I write here ? how make a filter for one definition?
return render(request, 'new_Order', {'form':form})
In your view (new_order), how do you know what customer is requesting the view?
What does your OrderForm look like?
Note - an Order is not a OneToOne relationship with Product, unless the customer may only ever place one order for that product. If the customer is able to place multiple orders for a Product, then it should be a ForeignKey relationship.
Likewise, can a Product only ever be ordered by only one Customer? If so, then you’re ok. But if not - if multiple Customers can place orders for the same product, then you do not want a ForeignKey relationship between Product and Customer. (You probably don’t want a relationship there at all - only between the Order and Customer.)
Hello everyone,
I think that I have found the solution of my problem and now I can nam it - Dependent Cascading Dropdown List. Here is the link to video
BUT (there is also “but” ) I have a problem that it does not work :((((
Please check what I do wrong:
<script>
$(document).ready(function()
{
var $select1=$('#select1'),
$select2=$('#select2'), - correct line
$options=$select2.find('option');
$select1.on('change',function()
{
$select2.html($options.filter('[value="'+this.value+'"]'));
}).trigger('change');
});
</script>
My question is about this whole code. How it works step by step, line by line…
e.g. as below I understand that I define the variebles
var $select1=$('#select1'),
$select2=$('#select2'),
and what about next lines?
I would like thank you so much for your time and support!!!
I am a beginner and I am still looking for teaching aids. I would like to understand as much as can to change later some code lines e.g. to add one more select3
Well, this is all JavaScript code - and I’m going to guess that it’s using jQuery as well. So those would be your resources for further research.
(No, I don’t have any specific recommendations for either.)