Ahmed Wagdi
Ahmed Wagdi

Reputation: 4371

checking if values are the same in django form

I have 2 models and am using 1 form to update both of them ,,

models.py

class Item(models.Model):
    item_category = models.ForeignKey(Category, on_delete="PROTECT")
    item_name = models.CharField(max_length=100, null=False)
    item_quantity = models.IntegerField(default=1)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    created_by = models.CharField(max_length=250, editable=False)

    def ___str__(self):
        return self.item_name


class ItemOut(models.Model):
    item_name = models.CharField(max_length=100, null=False)
    item_quantity = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True, editable=False)
    created_by = models.CharField(max_length=250, editable=False)

    def __str__(self):
        return self.item_name

here is the form that creates a new entry into Item model and only updates 1 field in the ItemOut table which is item_name

start with creating 2 forms in my forms.py

class AddItemForm(forms.ModelForm):
    class Meta:
        model = Item
        exclude = ('created_by', 'created_at', )
        labels = {
            'item_category': 'إسم الفئة ',
            'item_name': 'إسم الصنف',
            'item_quantity': 'الكمية',
        }


class AddItemOutForm(forms.ModelForm):
    class Meta:
        model = ItemOut
        fields = ('item_name',)
        labels = {
            'item_name': 'تأكيد إسم الصنف',
        }

then used 1 view function to render the form

views.py

def add_item(request):
    current_user = request.user
    if request.method == 'POST':
        new_item = AddItemForm(request.POST, request.FILES)
        new_item_out = AddItemOutForm(request.POST, request.FILES)
        if new_item.is_valid():
            if new_item_out.is_valid():
                item_out = new_item_out.save(commit=False)
                item_out.save()
                item = new_item.save(commit=False)
                item.created_by = current_user
                item.save()
            return redirect('cat')
    else:
        new_item = AddItemForm()
        new_item_out = AddItemOutForm()


    all_cats = Category.objects.all()
    cat_count = all_cats.count()
    item_count = Item.objects.all().count()
    all_units = Item.objects.aggregate(Sum('item_quantity'))['item_quantity__sum']

    context = {
        'all_units': all_units,
        'item_count': item_count,
        'cat_count': cat_count,
        'new_item': new_item,
        'current_user': current_user,
        'new_item_out': new_item_out,
               }

    return render(request, 'townoftech_warehouse/add_item.html', context)

Now I need to make sure that the entry field that creates item_name in the ItemOut table has the same value as the field that creates item_name in the Item table

Upvotes: 1

Views: 52

Answers (1)

Ashish Acharya
Ashish Acharya

Reputation: 3399

I don't think you need two forms here. Just use one form like this:

forms.py

class AddItemForm(forms.ModelForm):
    class Meta:
        model = Item
        exclude = ('created_by', 'created_at', )
        labels = {
            'item_category': 'إسم الفئة ',
            'item_name': 'إسم الصنف',
            'item_quantity': 'الكمية',
        }

view.py code to process the form

if request.method == 'POST':
    new_item = AddItemForm(request.POST, request.FILES)
    if new_item.is_valid():
        item = new_item.save(commit=False)
        item.created_by = current_user
        item.save()

        item_out = ItemOut.objects.create(
            item_name=request.POST.get('item_name'),
            created_by=current_user
        )
    return redirect('cat')

Upvotes: 2

Related Questions