模型形式内联验证和更改选择

时间:2016-05-19 11:10:22

标签: python django

我是Django / Python的新手,请原谅这个愚蠢。我有一个带有几个选择字段的模型表单。执行提交时,字段上有验证。这一切都运作良好。现在我想将验证更改为内联验证第一个选择字段的选择,然后根据为第一个选择字段选择的内容更改第二个选择字段。请原谅以下不好的例子,但你会明白这个想法

实施例 models.py:

fruit_list = (
    ('apple','Apple'),
    ('berries','Berries),
)

apple_color = (
    ('red', 'Red'),
    ('green', 'Green'),
)

berries_color = (
    ('red','Red'),
    ('blue', 'Blue'),
)

class fruit(models.Model):
    fruit = models.Charfield(max_lenght=7, choices=fruit_list)
    color = models.Charfield(max_lenght=5, choices=?????) ##Here Im looking for the answer on how to switches between the two options. Maybe just a normal form
    def __unicode__(self): ##python 3 use __str__
        return str(self.fruit)

形式:

class fruit(forms.ModelForm):
    class Meta:
        model = request
        fields = [ 'fruit', 'color' ]

另外,根据我的观点,我的验证在HTML帖子上。但是我如何进行内联验证,而不是我的帖子

views.py:

def home(request):
    if request.method == 'POST':
        form = fruit(request.POST, request.FILES)
        title = "Request Form"
        context = {
                "title": title,
                "form": form,
        }

        if form.is_valid():
            instance = form.save(commit=False) 
            return HttpResponseRedirect('/thanks/')
    else:
        form = fruit()
        title = "Request Form"
        context = {
                "title": title,
                "form": form,
        }
    return render(request, "home.html", context)

1 个答案:

答案 0 :(得分:0)

models.py

fruit_list = (
    ('apple','Apple'),
    ('berries','Berries),
)

apple_color = (
   ('red', 'Red'),
   ('green', 'Green'),
)

berries_color = (
    ('red','Red'),
    ('blue', 'Blue'),
)

class Fruit(models.Model):
    fruit = models.Charfield(max_lenght=7, choices=fruit_list)
    color = models.Charfield(max_lenght=5)  # Not a choice field

    def __unicode__(self):
        return str(self.fruit)

forms.py

class FruitForm(forms.ModelForm):
    class Meta:
        model = Fruit
        fields = [ 'fruit', 'color' ]

    def clean_color(self):
        data = self.cleaned_data
        fruit = data['fruit']
        color = data['color']

        if fruit == 'apple' and color not in dict(apple_color).keys():
            raise forms.ValidationError('Invalid selection')
        if fruit == 'berries' and color not in dict(berries_color).keys():            
            raise forms.ValidationError('Invalid selection')
        return data

fruit_list中选择项目后,触发一个ajax调用到另一个视图并传递所选项目(' apple' berries' berries')。

import simplejson
from django.http import HttpResponse

def fruit_color_list(request):
    fruit = request.GET.get('fruit')
    if fruit == apple:
        fruit_list = apple_color
    else:
        fruit_list = berries_color

    return HttpResponse(simplejson.dumps(fruit_list))

使用javascript在水果颜色列表中填充此响应。剩下的流量是一样的。

def home(request):
    if request.method == 'POST':
        ....
        ....
        if form.is_valid():
            instance = form.save() 
            return HttpResponseRedirect('/thanks/')
    else:
        ....
    return render(request, "home.html", context)