Get 2 objects in updateview django
I have have a template where i want to make update on 2 different models, the first one is the order model , and teh second is the customer model. In the template i've got the 2 forms, but the problem is that i cant get informations about the customer in the customer form
models.py
class Customer(models.Model):
full_name = models.CharField(max_length=150)
address = models.CharField(max_length=1500, null=True)
phone = models.CharField(max_length=20)
city = models.CharField(max_length=100)
email = models.EmailField(null=True)
def __str__(self):
return self.full_name
class Order (models.Model):
product = models.ManyToManyField(Product, through='OrderProduct')
customer = models.ForeignKey(Customer, on_delete=models.CASCADE,)
quantity = models.IntegerField(default=1)
status = models.TextField(choices=ORDER_STATUS, default='Pending')
def __str__(self):
return 'Order n°: ' + str(self.id)
views.py
class OrderUpdateView(LoginRequiredMixin, RedirectToPreviousMixin, UpdateView):
model = Order
form_class = OrderManageForm
second_form_class = CustomerForm
template_name = 'dashboard/order_details.html'
login_url = '/login/'
def get_object(self):
return Order.objects.get(id=self.kwargs['order_id'])
def get_context_data(self, **kwargs):
context = super(OrderUpdateView, self).get_context_data(**kwargs)
if 'form' not in context:
context['form'] = self.form_class()
if 'customer' not in context:
context['customer'] = self.second_form_class()
return context
How could i get the customer object ?