Django modelform不保存输入选择并且不返回错误 [英] Django modelform not saving input choices and not returning errors

查看:81
本文介绍了Django modelform不保存输入选择并且不返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仅在没有字段可供选择的情况下才起作用的模型形式(将输入数据保存到数据库中)。当我介绍选择项时,我没有出现任何错误,该表格似乎有效,但没有保存任何内容。
我已经仔细阅读了文档,并且没有返回任何有用的信息。

I have a modelform that only works(saves input data to database) if none of the fields has choices. When i introduce choices, i don't get any errors and the form seems to be valid but nothing gets saved. I have combed through the documentation and i am not returning anything useful.

我坚信我需要做更多的工作才能获得选定的输入选择还是我需要在模型类中添加一些方法。请为我指出正确的方向。

I am convinced that i need to do more in my views to get the selected input choices or i need to add a few methods to the model class. Please point me in the right direction.

这是我的模型:

class OpeningHours(models.Model):
'''
'''
    class Meta:
        verbose_name = 'Opening Hour'
        verbose_name_plural = 'Opening Hours'
    #######################################################
    mytime = Bizhours()
    ################################################ 
    id = models.AutoField(primary_key=True)
    company =models.CharField(max_length=100, null=True, blank=True)
    weekday = models.CharField(max_length=100, choices=mytime.getweekdays(), default='Monday', null=True)
    fromHour = models.CharField(max_length=100, null=True, blank=True)
    fromMinute = models.CharField(max_length=100, null=True, blank=True)
    toHour = models.CharField(max_length=100, null=True, blank=True)
    toMinute = models.CharField(max_length=100, null=True, blank=True)
    '''
    id = models.AutoField(primary_key=True)
    company = models.ForeignKey(Company)
    weekday = models.IntegerField(choices=mytime.getweekdays())
    fromHour = models.TimeField(choices=mytime.gettime12())
    fromMinute = models.TimeField(choices=mytime.getminutes())
    toHour = models.TimeField(choices=mytime.gettime12())
    toMinute = models.TimeField(choices=mytime.getminutes())
    '''


    def __str__(self):
        return "%s %s (%s - %s)" % (self.company, self.weekday, self.fromHour, self.toHour)

我的意见

@login_required
def addprofile(request):
current_user = request.user
#OpeningHoursFormSet = modelformset_factory(OpeningHours, form=OpeningHoursForm,extra=1)

if request.session['entry_count'] > 1:
    messages.success( request, 'You can only create two business profiles now' )
    return HttpResponseRedirect( reverse('home') )
else:
    if request.method == 'POST':
        form = OpeningHoursForm(request.POST)

        if form.is_valid():
            model_instance = form.save(commit=False)
            model_instance.company ="thiscompany"
            model_instance.weekday = request.POST.get('weekday') 
            model_instance.save()
        else:
            print("problems saving edited form")

        return HttpResponseRedirect('/bizprofile/success')
    else: 
        form = OpeningHoursForm() 
        context = {'form': form}    
    return render_to_response('bizprofile/addprofile.html', context, context_instance=RequestContext(request))

这是表单

{% extends "bizprofile/bizprofilebase.html" %}

{% block content %}

{% if form.subject.errors %}
<ol>
{% for error in form.subject.errors %}
    <li><strong>{{ error|escape }}</strong></li>
{% endfor %}
</ol>
{% endif %}

{% if user.is_authenticated %}
    <p>Welcome, {{ user.get_username }}. Thanks for logging in.</p>

    <form method="post" action="">
    {% csrf_token %}
    <table>
    {{form}}
    </table>
    <input type="submit" value="Submit Form"/>
    </form>

{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}
{% endblock %}


推荐答案

问题在于,OP将 CharField 用作工作日数据类型,但是从函数返回的选择定义为整数。由于它们不兼容,因此无法保存数据。

The problem lies in the fact that OP is using CharField for weekday data type, but the choices returned from a function are defined as integers. Since they are not compatible, the data could not be saved.

这篇关于Django modelform不保存输入选择并且不返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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