Django Form If条件在view.py中有2个实例 [英] Django Form If condition in view.py with 2 instance

查看:87
本文介绍了Django Form If条件在view.py中有2个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要保存在Django中以表格形式输入的数据,我试图使它像这样
,我将其放入模型中。py

TO SAVE DATA that is inputted in form in Django i tried tomake it like this I put this in my model.py

class Item(models.Model):
    CATEGORY = (
        ('Gudang Kering', 'Gudang Kering'),
        ('Gudang Basah','Gudang Basah'),
        )
    name = models.CharField(max_length=200,null= True)
    stock = models.IntegerField(default='0', blank=False, null=True)
    category = models.CharField(max_length=200,null= True,choices=CATEGORY)
    reorderlevel = models.IntegerField(default='0', blank=False, null=True)
    maxreorderlevel = models.IntegerField(default='0', blank=False, null=True)
    description = models.CharField(max_length=200,null= True, blank= True)
    date_created = models.DateTimeField(auto_now_add= True)
    tags = models.ManyToManyField(Tag)
    
    def __str__(self):
        return self.name

class Issue(models.Model):
    STATUS = (
        ('Pending', 'Pending'),
        ('Granted','Granted'),
        ('Denied','Denied'),
        )
    customer = models.ForeignKey(Customer, null=True, on_delete= models.SET_NULL)
    item = models.ForeignKey(Item, null=True, on_delete= models.SET_NULL)
    quantity = models.IntegerField(default='0', blank=False, null=True)
    date_created = models.DateTimeField(auto_now_add=True, auto_now=False)
    status = models.CharField(max_length=200,null= True, choices=STATUS)

然后在view.py中定义这样的形式

Then in view.py i define the form like this

def updateIssue(request, pk):
    issue = Issue.objects.get(id=pk)
    item = Item.objects.all()
    form = UpdateIssueForm(instance=issue)
    if request.method == 'POST':
        form = UpdateIssueForm(request.POST,instance=issue)
        #print ('printing:',request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            if instance.status == 'Granted':
                item.stock -= instance.quantity
                instance.save()
                item.save()
            else:
        instance.save()

        return redirect('/')
    

    context = {'form':form}
    return render(request,'accounts/issue_form.html',context)``

目标

如果instance == Granted
,item.stock将减少instance.quantity
的数量并将被保存。

else

实例将被保存而不会影响第二个模型的库存

The Goal
if instance == "Granted"
the item.stock will be decreased on the amount of instance.quantity and will be saved.
else
instance will just be saved without affecting the stock from the 2nd model

错误

    item = Item.objects.all()

即使调用item.stock也具有0属性,即使我在该表的数据库中有输入数据

even when called the item.stock have 0 attribute even when i have input data in database for that table

推荐答案

无需获取 Item ,因为我们可以访问 Item 使用问题对象(例如 issue.item )与问题相关。这只是来自 docs 的示例:

There is no need to get Item since we can access the Item related to Issue using the Issue object like issue.item. This is just an example from docs:

文章模型具有一个字段报告者,该报告者是实现为Reporter模型的ForeignKey。

Article model has a field reporter which is a ForeignKey realted to Reporter model. Using Article object the Reporter object is accessed.

>>> new_article = r.article_set.create(headline="John's second story", pub_date=date(2005, 7, 29))
>>> new_article
<Article: John's second story>
>>> new_article.reporter
<Reporter: John Smith>
>>> new_article.reporter.id
1

因此,我们可以访问 Item 使用问题

Like so, we can access the Item using Issue

def updateIssue(request, pk):
    issue = Issue.objects.get(id=pk)  # we have our Issue here
    form = UpdateIssueForm(instance=issue)
    if request.method == 'POST':
        form = UpdateIssueForm(request.POST,instance=issue)

        if form.is_valid():
            instance = form.save(commit=False)
            if instance.status == 'Granted':
                issue.item.stock -= instance.quantity   # access Item by using Issue object's related field with name item
                issue.item.save() # save the Item first
                instance.save() # then the Issue
            else: 
                instance.save()

        return redirect('/')
    

    context = {'form':form}
    return render(request,'accounts/issue_form.html',context)

这篇关于Django Form If条件在view.py中有2个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆