Django 关于表单的问题

发布于 2024-12-04 23:29:57 字数 1244 浏览 0 评论 0原文

你好,我有一个 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?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦明 2024-12-11 23:29:57

为此,您应该使用 javascript。如果您使用 {{ form.as_table }} 那么通过使用 jQuery 它将看起来像这样:

<script type="text/javascript">
    $(function() {
        showOrHideLocation();
        $('#id_transaction_type').change(showOrHideLocation);
    });
    function showOrHideLocation() {
        if ($('#id_transaction_type option:selected[value=1]').length) {
            $('#id_country_type').parents('tr:first').show();
        }
        else {
            $('#id_country_type').parents('tr:first').hide();
        }
    }
</script>

然后只需在表单中验证您的结果。

You should use javascript for this. If you're using {{ form.as_table }} then by using jQuery it'll look something like this:

<script type="text/javascript">
    $(function() {
        showOrHideLocation();
        $('#id_transaction_type').change(showOrHideLocation);
    });
    function showOrHideLocation() {
        if ($('#id_transaction_type option:selected[value=1]').length) {
            $('#id_country_type').parents('tr:first').show();
        }
        else {
            $('#id_country_type').parents('tr:first').hide();
        }
    }
</script>

Then just validate your result in your forms.

风铃鹿 2024-12-11 23:29:57

这个问题与 django 无关。您应该通过 javascript 来做到这一点:隐藏或显示基于

唯一的事情是表单验证。您应该按照您的情况执行它:

   from django.core.exceptions import ValidationError

   class Transaction(models.Model):
       # your fields

       def clean(self):
           if self.transaction_type == 2 and self.country_type:
               raise ValidationError(u'You must not fill location with "purchase" option.')

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:

   from django.core.exceptions import ValidationError

   class Transaction(models.Model):
       # your fields

       def clean(self):
           if self.transaction_type == 2 and self.country_type:
               raise ValidationError(u'You must not fill location with "purchase" option.')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文