I have a little problem with “copy” input informations in forms before it will be save using CreateView and other Class-Based-Views.
My model.py:
from django.db import models
from stock.models import Material, Dimensionmat, Typemat, Grademat, Tensil
from clients_suppliers.models import Client
# Create your models here.
class NumberingofProduct(models.Model):
client = models.ForeignKey(Klient, on_delete=models.SET_NULL, null=True)
no_drowing_prod = models.CharField('Number drawing, max_length=30, unique=True, blank=True, null=True)
no_prod_clients = models.CharField('Client number of product, max_length=30, unique=True, blank=True, null=True)
no_prod_mine = models.CharField('Mine number of product', max_length=30, unique=True, blank=True, null=True)
class Meta:
abstract = True
ordering = ['no_drowing_prod']
class NumberofProductatStock(NumberingofProduct):
class Meta:
ordering = ['no_drowing_prod']
class NumberofProduct(NumberingofProduct):
numberofproductatstock = models.ForeignKey(NumberofProductatStock, on_delete=models.SET_NULL, null=True)
class Meta:
ordering = ['no_drowing_prod']
class BaseInfoProduct(models.Model):
""" Class abstract"""
client = models.ForeignKey(Client, on_delete=models.SET_NULL, null=True)
numberofproductatstock = models.ForeignKey(NumberofProductatStock, on_delete=models.SET_NULL, null=True)
numberofproduct = models.ForeignKey(NumberofProduct, on_delete=models.SET_NULL, null=True)
name_spr = models.CharField(max_length=40, blank=True, null=True)
material = models.ForeignKey(Material, on_delete=models.SET_NULL, null=True)
dimensiomat = models.ForeignKey(Dimensionmat, on_delete=models.SET_NULL, null=True)
typemat = models.ForeignKey(Typemat, on_delete=models.SET_NULL, null=True)
gradekmat = models.ForeignKey(Grademat, on_delete=models.SET_NULL, null=True)
tensil = models.ForeignKey(Tensil, on_delete=models.SET_NULL, null=True)
weight = models.DecimalField(max_digits=9, decimal_places=3, blank=True, null=True, help_text='Unit: grams')
class Springcompression(BaseInfoProduct):
RIGHT = right
LEFT = left
DISCRETIONARY = 'discretionary'
STATUS = [
(RIGHT, 'Right'),
(LEFT, 'Left'),
(DISCRETIONARY, 'DISCRETIONARY '),
]
kierunek_zwijania = models.TextField(max_length=7, choices=STATUS, default=RIGHT)
dz = models.PositiveSmallIntegerField('Dz', blank=True, null=True)
dz_tol_plus = models.DecimalField('Dz+', max_digits=4, decimal_places=2, blank=True, null=True)
dz_tol_minus = models.DecimalField('Dz-', max_digits=4, decimal_places=2, blank=True, null=True)
dp = models.PositiveSmallIntegerField('Dp', blank=True, null=True)
dp_tol_plus = models.DecimalField('Dp+', max_digits=4, decimal_places=2, blank=True, null=True)
dp_tol_minus = models.DecimalField('Dp-', max_digits=4, decimal_places=2, blank=True, null=True)
dw = models.PositiveSmallIntegerField('Dw', blank=True, null=True)
dw_tol_plus = models.DecimalField('Dw+', max_digits=4, decimal_places=2, blank=True, null=True)
dw_tol_minus = models.DecimalField('Dw-', max_digits=4, decimal_places=2, blank=True, null=True)
lo = models.PositiveSmallIntegerField('L0', blank=True, null=True)
lo_tol_plus = models.DecimalField('L0+', max_digits=4, decimal_places=2, blank=True, null=True)
lo_tol_minus = models.DecimalField('L0-', max_digits=4, decimal_places=2, blank=True, null=True)
forms.py
class SpringcompressionForm(ModelForm):
class Meta:
model = Springcompression
exclude = ['numberofproduct']
def __init__(self,*args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['dimensiomat'].queryset = Dimensionmat.objects.none()
self.fields['Typemat'].queryset = Typemat.objects.none()
self.fields['Grademat'].queryset = Grademat.objects.none()
views.py
class SpringcompressionListView(ListView):
model = Springcompression
context_object_name = 'springcompressions'
class SpringcompressionCreateView(CreateView):
model = Springcompression
form_class = SpringcompressionForm
success_url = reverse_lazy('springcompression_changelist')
class SpringcompressionUpdateView(UpdateView):
model = Springcompression
form_class = SpringcompressionForm
success_url = reverse_lazy('springcompression_changelist')
Please focus at two atrributes from BaseInfoProduct: numberofproductatstock and numberofproduct.
Lets assume that I would like to create my product first time ever for new client Coca-cola. It is the same numberofproduct as numberofproductatstock. So I do not want to burdon the user to write the same numbers into numberofproduct. I would like to write one view for automatically copy this numbers from numberofproductatstock into numberofproduct. And I do not show at this view field: numberofproduct.
Second example I assume that I could create the same product (the same numberofproductatstock) but with diffrent numberofproduct but for another client- Pepsi. The second view (and forms with all fields) should has both fields. At first numberofproductatstock to choose the number and then field for new numberofproduct for another client- Pepsi.
If I not use Class-Based-Views I think that I could write the function:
def copy_numbers_to_numberofproduct (request):
form_product = SpringcompressionForm(request.POST or None)
if form_product.is_valid():
product = form_product.save(commit=False)
product.numberofproduct.no_drawing_prod = product.numberofproductatstoct.no_drawing_prod
product.numberofproduct.no_prod_clients = product.numberofproductatstock.no_prod_clients
product.numberofproduct.no_prod_mine = product.numberofproductatstock.no_prod_mine
product.save()
return redirect('springcompression_changelist')
context = {'form': form_product}
return render(request, 'springcompression_form.html', context)
Please tell me would it be a good function? and what I should to do while I am using Class-Based-Views?