2016-03-24 15 views
2

Yerleşik sınıf tabanlı görünümler yerine işlev tabanlı görünümler kullanarak viewflow.io ile çok basit bir test uygulaması denemek. Amaçlanan fikir, bir ürünün eklendikten sonra onaylanmasıdır (iki farklı görünüm/form aracılığıyla). İki sorunları Ben yolculuk devam edemez vardır:.Django Viewflow değişkenleri işlev tabanlı görüntülemeye geçirme

  1. Ben onay yaptığını kullanıcı onaylayıp içindir şeyin özetini görebilmesi için (onay görünümüne Product geçmek istiyorum ben değilim Ben flows.py yılında flow.View aracılığıyla product_pk geçen çalıştı ama bu hatayla sonuçlanır ve bunu bırakırsan o onay görünümü oldukça mevcut ürün daha tüm kayıtları günceller
  2. flow.If kapısı akışlarında - emin nasıl yapılacağını.. Ürün üzerindeki onaylanmış alanın kontrol edilip edilmediğine bakılmaksızın her zaman doğru görünür. İdeal olarak onayın, Ürün modelinde süreçten ziyade kaydedildiğini umuyorum. model

Muhtemelen süper temel hata/kavram Ben eksik - herhangi bir yardım takdir edilecektir.

class Product(models.Model): 
    name = models.CharField(max_length=30) 
    quantity = models.IntegerField() 
    approved = models.BooleanField(default=False) 

    def __str__(self): 
     return self.name 

class ProductProcess(Process): 
    product = models.ForeignKey(Product, blank=True, null=True) 

    def approved(self): 
     return self.product.approved 


class ProductTask(Task): 
    class Meta: 
     proxy = True 

class ProductFlow(Flow): 
    process_cls = ProductProcess 
    task_cls = ProductTask 

    start = flow.Start(start_process).Next(this.approve) 

    approve = flow.View(approve_product).Next(this.checkapproval) 

    checkapproval = flow.If(cond=lambda p: p.approved()) \ 
     .OnFalse(this.approve) \ 
     .OnTrue(this.end) 

    end = flow.End() 

ca şekilde views.py

@flow_start_view() 
def start_process(request, activation): 
    activation.prepare(request.POST or None,) 
    form = ProductForm(request.POST or None) 

    if form.is_valid(): 
     Product.objects.create(
      name = form.cleaned_data['name'], 
      quantity = form.cleaned_data['quantity'] 
     ) 
     activation.done() 
     return redirect('/test') 

    return render(request, 'viewflowtest/product.html', {'activation': activation, 'form': form}) 

@flow_view() 
def approve_product(request, activation): 
    activation.prepare(request.POST or None,) 
    form = ApproveProductForm(request.POST or None) 

    if form.is_valid(): 
     Product.objects.update(
      approved = form.cleaned_data['approved'] 
     ) 
     activation.done() 
     return redirect('/test') 
    return render(request, 'viewflowtest/product.html', {'activation': activation, 'form': form}) 

olarak flows.py olarak models.py olarak lled çok temel bir ModelForm sınıfıdır ve URL'ler proje GitHub sayfalarındaki demo uygulamalarında anlatıldığı gibidir. Şablon, {{ activation.management_form }} etiketine sahiptir.

cevap

0

Her şeyden önce, ürünü ve süreci birbirine bağlamanız gerekir. ProductForm Yani onay görünümü ise ::

@flow_view() 
def approve_product(request, activation): 
    activation.prepare(request.POST or None,) 
    form = ApproveProductForm(request.POST or None, instance=activation.process.product) 

    if form.is_valid(): 
     form.save() # here is the approved field is updated 
     activation.done() 
     return redirect('/test') 
    return render(request, 'viewflowtest/product.html', {'activation': activation, 'form': form}) 

şekilde tekrar yazılabilir ModelForm

if form.is_valid(): 
    product = form.save() 
    activation.process.product = product 
    activation.done() # here is new process instance created and saved to db 

Yani eğer başlangıç ​​görünümünde, sen

if form.is_valid(): 
    product = Product.objects.create(
     name = form.cleaned_data['name'], 
     quantity = form.cleaned_data['quantity'] 
    ) 
    activation.process.product = product 
    activation.done() 

hatta daha iyisini yapabiliriz ek olarak, işlev tabanlı görünümler içeren görünüm akışına bir göz atabilirsiniz - https://github.com/viewflow/cookbook/blob/master/viewflow_customization/customization/parcel/views.py

+0

Kickass! :) Süper hızlı cevap için çok teşekkür ederim (umarım, her engelle ilgili sorular göndermeden Viewflow'un geri kalanını halledebilirim). –

İlgili konular