S
S
Sergey Y2021-05-28 11:37:16
Django
Sergey Y, 2021-05-28 11:37:16

How to implement BooleanField change on form save in Django?

class Zakaz(models.Model):
  pokupatel_name = models.CharField(max_length=150, verbose_name= 'Покупатель')
  pokupatel_email = models.EmailField(verbose_name= 'Покупатель email')
  phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$',message=
  "Phone number must be entered in the format: '+999999999'. Up to 10 digits allowed.")
  pokupatel_phone = models.CharField(validators=[phone_regex], max_length=12, verbose_name= 'Телефон покупателя') # validators should be a list
  work = ForeignKey(Usluga, on_delete=PROTECT, verbose_name= 'Вид работы', related_name="work")
  date = DateTimeField(auto_now_add=True,verbose_name= 'Дата оформления заказа')
  v_rabote = models.BooleanField(default=False, verbose_name= 'Заказ в работе')
  def __str__(self) :
    return "Заказ №" + str(self.id) + " - " + str(self.work)
  class Meta:
    verbose_name= 'Заказ'
    verbose_name_plural= 'Заказы'

class NaRabotu(Instrument,Material):
  objects = NaRabotuManager()#Забирает только отсортированные Менеджером объекты!!!
  zakaz = OneToOneField (Zakaz, on_delete=PROTECT, db_index=True, verbose_name= 'Заказ',related_name="zakaz")
  rabotnik = ForeignKey(Rabotnik, on_delete=PROTECT, verbose_name= 'Работник', related_name="rabotnik")
  date = DateTimeField(auto_now_add=True,verbose_name= 'Принято к исполнению')
  def __str__(self):
    return str(self.zakaz)
  class Meta:

    verbose_name= 'В работе'
    verbose_name_plural= 'В работе'
    ordering = ['-date']

class ZakazAddView(CreateView):
  template_name = 'rabota/add_zakaz.html'
  model = Zakaz 
  fields =('pokupatel_name','pokupatel_email', 'pokupatel_phone', 'work')
  success_url = reverse_lazy ('rabota:index')

#Обработка формы в работу:
class RabotaAddView(CreateView):
  template_name = 'rabota/add_rabota.html'
  form_class = NaRabotuForm
  success_url = reverse_lazy ('rabota:index')

Can't implement BooleanField change in order while saving form RabotaAddView!!!!!!!! with logic! The buyer creates an order, in the order the BUL field with Fales by default, and when the order goes to work and its executor is assigned, it is necessary that in the given order it becomes True when the object is saved on the work registration

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-06-02
@sergey_1990

Add Method to WorkAddView

def form_valid(self, form):
    response = super().form_valid(form)

    Zakaz.objects.filter(id=self.object.zakaz_id).update(v_rabote=True)

    return response

PS Do not use transliteration in programming NEVER, use translate.google.com, www.lingvolive.com and www.deepl.com and another useful resource for working with CBV https://ccbv.co.uk/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question