【问题标题】:Update a particular field in django forms and not be able to edit the others更新 django 表单中的特定字段并且无法编辑其他字段
【发布时间】:2020-08-22 13:07:47
【问题描述】:

我正在尝试创建一个费用报销应用程序,我创建了一个表单,用户可以在其中报销费用,当管理员登录到图片中的以下网页时,网页会迭代所有具有 claim_status == 的索赔Null,我希望管理员能够接受或拒绝下拉列表中的声明,并更新数据库和网页中该对象的值以刷新并且该声明不再显示

我为 ExpenseClaim 模型提供了 Null 的初始值,当用户填写费用报销表时,他看不到 claim_status 字段

请看我的模型

class ExpenseClaim(models.Model):
    reference_number        = models.AutoField(primary_key=True)
    employee_name           = models.CharField(max_length=150)
    employee_ID             = models.CharField(max_length=150)
    claim_initiating_date   = models.DateTimeField(auto_now=True)
    invoice_date            = models.DateField()
    invoice_location        = models.CharField(max_length=150)
    biller_name             = models.CharField(max_length=150)
    invoice_category        = models.CharField(max_length=150)
    price                   = models.DecimalField(decimal_places=2, max_digits=10, null=False , default=0.00) 
    GST                     = models.DecimalField(decimal_places=2 ,max_digits=10, null=False , default=0.00) 
    CGST                    = models.DecimalField(decimal_places=2 ,max_digits=10, null=False , default=0.00)
    IGST                    = models.DecimalField(decimal_places=2, max_digits=10, null=False , default=0.00)
    Total_amount            = models.DecimalField(decimal_places=2 ,max_digits=10, null=False , default=0.00)
    mode_of_payement        = models.CharField(max_length=20)
    Payment_reference       = models.BooleanField()
    invoice                 = models.FileField()
    claim_status            = models.CharField(default=None , max_length=10)
    payment_status          = models.CharField(default= None , max_length=10)

以下是我的模型字段,我尝试创建一个新表单,但这只是在数据库中创建了一个新对象,我想更新现有的对象

invoice_category_choices =[
    ('food',"food"),
    ('travel',"travel"),
    ('accodmodation',"accomodation"),
    ('fuel',"fuel"),
    # ('others',"others"),

]

claim_status_choices = [
    ('accepted' , 'accepted'),
    ('rejected' , 'rejected')
]

payment_status_choices =[
    ('paid' , 'paid'),
    ('notpaid' , 'notpaid') 
]

class ExpenseClaimForm(forms.ModelForm):
    class Meta:
        model = ExpenseClaim
        exclude = [
            "employee_name",
            "employee_ID",
            "claim_status",
            "payment_status"
        ]  
        widgets = {
        'invoice_date': forms.DateInput(format=('%m/%d/%Y'), attrs={'class':'form-control', 'placeholder':'Select a date', 'type':'date'}),
        'invoice_category':forms.Select(choices= invoice_category_choices),
        'claim_status':forms.Select(choices= claim_status_choices)
        
    }
            
class ClaimStatusForm(forms.ModelForm):
    class Meta:
        model = ExpenseClaim
        fields = [
            "claim_status",
            
        ]
        widgets = {
        'claim_status':forms.Select(choices= claim_status_choices)
        
    }  

【问题讨论】:

  • 你只想更新claim status ??还是全部更新??
  • @RiyasAc only claim_status rest fields only display
  • 请添加视图和html

标签: django-models django-forms django-views django-templates


【解决方案1】:

forms.py

class ClaimStatusForm(forms.ModelForm):
    class Meta:
        model = ExpenseClaim
        fields = [
            "claim_status",
            
        ]
        widgets = {
        'claim_status':forms.Select(choices= claim_status_choices)
        
    }  
        

查看 .py

def admin_update_view(request , reference_number):  
  post = get_object_or_404(ExpenseClaim , reference_number=reference_number)
  form = ClaimStatusForm(instance=post)

  if request.method == 'POST':
    post = get_object_or_404(ExpenseClaim , reference_number=reference_number)
    form = ClaimStatusForm(request.POST)
    if form.is_valid():
      claim_status = form.cleaned_data.get('claim_status')

      post.claim_status = claim_status
      post.save()
      return redirect('/admin/')

  context  = { "form":form , "initialize_context" : initialize_context(request)}
  return render(request , 'graphauth/adminupdate.html' , context)

urls.py

 path('adminupdate/<int:reference_number>', views.admin_update_view , name='adminupdate'),

admin.html

<a href="/adminupdate/{{items.reference_number}}" class=""btn btn-secondary">update</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-04-18
    • 2023-03-21
    • 2021-09-12
    • 2014-06-02
    • 1970-01-01
    相关资源
    最近更新 更多