Django 关于表单的问题
你好,我有一个 Django 应用程序,它有一个可以添加销售和购买的表单。我想用它做出改变。如果用户选择购买而不是销售,我不希望他们有国家类型(位置)。事实上,我不希望它们有任何购买价值。
因此,总而言之,所有销售都应该有选择国家/地区类型的选项,而所有购买都不应该有此选项。我该怎么做?
models.py
from management_sys.vat import models
TRANSACTION_TYPE_CHOICES = ((1, 'sale'), (2, 'purchase'),)
COUNTRY_TYPE_CHOICES = ((1, 'UK'), (2, 'EU'),)
class Transaction(models.Model):
transaction_type = models.Integerfield(verbose_name = "Type", choices = TRANSACTION_TYPE_CHOICES)
country_type = models.Integerfield(verbose_name = "Location", choices = COUNTRY_TYPE_CHOICES)
date = models.Datefield()
vat_period = models.Datefield()
amount = models.DecimalField(max_digits=20, decimal_places=2)
vat = models.DecimalField(max_digits=20, decimal_places=2)
description models.TextField(MAX_LENGTH = 400)
def __unicode__(self):
return unicode(self.amount)
forms.py
from management_sys.vat.models import *
from django import forms
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
Hello I have a django app that has a form that you can add sales and purchase. There is a change I want to make with it. If a user chooses a purchase instead of a sale, I do not want them to have a country type (location). In fact I do not want them to have any value for a purchase.
So to summerise, All sales should have an option to select a country type, while all puchases should not be given this option. How can I do this?
models.py
from management_sys.vat import models
TRANSACTION_TYPE_CHOICES = ((1, 'sale'), (2, 'purchase'),)
COUNTRY_TYPE_CHOICES = ((1, 'UK'), (2, 'EU'),)
class Transaction(models.Model):
transaction_type = models.Integerfield(verbose_name = "Type", choices = TRANSACTION_TYPE_CHOICES)
country_type = models.Integerfield(verbose_name = "Location", choices = COUNTRY_TYPE_CHOICES)
date = models.Datefield()
vat_period = models.Datefield()
amount = models.DecimalField(max_digits=20, decimal_places=2)
vat = models.DecimalField(max_digits=20, decimal_places=2)
description models.TextField(MAX_LENGTH = 400)
def __unicode__(self):
return unicode(self.amount)
forms.py
from management_sys.vat.models import *
from django import forms
class TransactionForm(forms.ModelForm):
class Meta:
model = Transaction
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您应该使用 javascript。如果您使用 {{ form.as_table }} 那么通过使用 jQuery 它将看起来像这样:
然后只需在表单中验证您的结果。
You should use javascript for this. If you're using {{ form.as_table }} then by using jQuery it'll look something like this:
Then just validate your result in your forms.
这个问题与 django 无关。您应该通过 javascript 来做到这一点:隐藏或显示基于
唯一的事情是表单验证。您应该按照您的情况执行它:
This question is not really django related. You should do that via javascript: hide or show some field base on <select> value.
The only thing is form validation. You should perform it like that in your case: