Answer the question
In order to leave comments, you need to log in
How to use function from model in CBV view?
I want to use the approv() method in order to add to the order the executor who left a review for a specific order, through which form or in what way can this be done on the order_detail.html page?
class Order(models.Model):
customer = models.ForeignKey(Customer,
related_name='orders',
on_delete=models.CASCADE)
title = models.CharField(max_length=250)
text = models.TextField()
image = models.ImageField(upload_to='orders/%Y/%m/%d',
blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
executer = models.ForeignKey(Executer,
related_name='orders',
on_delete=models.SET_NULL,
blank=True,
null=True)
condition = models.BooleanField(default=False)
condition_success = models.BooleanField(default=False)
moderation = models.BooleanField(default=False)
slug = models.SlugField()
def __str__(self):
return self.title
<b>def approv(self, executer_app):
self.executer = executer_app
self.condition = True
self.save()</b>
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super(Order, self).save(*args, **kwargs)
class ResponseOrder(models.Model):
title = models.CharField(max_length=250)
order = models.ForeignKey(Order,
related_name='responses_orders',
on_delete=models.CASCADE)
executer = models.ForeignKey(Executer,
related_name='responses_orders',
on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
condition = models.BooleanField(default=False)
def __str__(self):
return self.title
class AbstractProfile(models.Model):
user = models.ForeignKey(User,
related_name='%(class)s_related',
on_delete=models.CASCADE)
first_name = models.CharField(max_length=70)
last_name = models.CharField(max_length=70)
phone = PhoneNumberField(blank=True, unique=True)
email = models.EmailField(unique=True)
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField()
class Meta:
abstract = True
def __str__(self):
return "{} {}".format(self.first_name,
self.last_name)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.user.username)
super(AbstractProfile, self).save(*args, **kwargs)
class Customer(AbstractProfile):
pass
class Executer(AbstractProfile):
description = models.CharField(max_length=250)
class OrderDetailView(FormMixin, DetailView):
model = Order
context_object_name = 'order'
template_name = 'orders/manage/order/order_detail.html'
form_class = ResponseForm
def get_success_url(self):
return reverse('order:order_detail', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(OrderDetailView, self).get_context_data(**kwargs)
context['responses'] = ResponseOrder.objects.filter(order=self.object)
context['form'] = self.get_form()
return context
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.instance.executer = Executer.objects.get(user=self.request.user)
form.instance.order = self.object
form.save()
return super(OrderDetailView, self).form_valid(form)
class OrderListView(ListView):
model = Order
context_object_name = 'orders'
template_name = 'orders/manage/order/order_list.html'
class OrderCreateView(CreateView):
model = Order
fields = ['title', 'text', 'image', 'price']
success_url = '/'
template_name = 'orders/manage/order/order_create.html'
def form_valid(self, form):
# customer =
form.instance.customer = Customer.objects.get(user=self.request.user)
return super().form_valid(form)
<h3>{{ order.title }}</h3>
<form class="" method="post">{% csrf_token %}
{{form.as_p}}
<input type="submit" name="" value="Отправить">
</form>
<hr>
<input type="submit" name="" value="">
{% for response in responses%}
{{response}}
<br>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question