我正在尝试使用现有数据转发模型.该模型有一个新的字段,其约束唯一= True和null = False.当我做
./manage.py schemamigration myapp --auto
Run Code Online (Sandbox Code Playgroud)
South让我通过询问为新字段指定默认值:
Specify a one-off value to use for existing columns now
Run Code Online (Sandbox Code Playgroud)
通常我将其设置为None,但由于此字段必须是唯一的,我想知道是否可以通过以下方式传递South的唯一值:
>>> import uuid; uuid.uuid1().hex[0:35]
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误信息
! Invalid input: invalid syntax
Run Code Online (Sandbox Code Playgroud)
在通过命令行迁移时是否可以传递South随机唯一默认值?
谢谢.
作为我的Django视图中的表单向导的一部分,我使用的是Formset.每个步骤的向导表单都声明如下:
UserFormSet = modelformset_factory(account_models.MyUser,
form=account_forms.MyUserForm,
extra=5,
max_num=10,
can_delete=True)
FORMS = [('userchoice', UserChoiceForm),
('user', UserFormSet),]
TEMPLATES = {'userchoice': "account/userchoice.html",
'user': "account/user.html",}
Run Code Online (Sandbox Code Playgroud)
我想要实现的是:在UserChoiceForm(第一步)中,可以设置所需用户的数量.我想使用此值动态设置UserFormSet上的额外属性,以便在第二步中只显示所需数量的表单.
我试图通过覆盖向导的get_form()方法来做到这一点:
class MyUserWizard(SessionWizardView):
def get_form(self, step=None, data=None, files=None):
form = super(MyUserWizard, self).get_form(step, data, files)
# Determine the step if not given
if step is None:
step = self.steps.current
if step == 'user':
# Return number of forms for formset requested
# in previous step.
userchoice = self.get_cleaned_data_for_step('userchoice')
num_users = userchoice['num_users']
CoFunderFormSet.extra …Run Code Online (Sandbox Code Playgroud) 我是Django的新手,对表单处理过程中的验证步骤感到有些困惑.我知道默认情况下需要所有表单字段类型(在我的情况下是ModelForm).我假设Django会引发VaidationError,以防所需的表单字段留空而不调用表单的clean方法.
这就是为什么我没有检查是否在以下clean()方法中设置了任何数据:
def clean(self):
date = self.cleaned_data.get('date')
time_start = self.cleaned_data.get('time_start')
time_end = self.cleaned_data.get('time_end')
user_type = self.cleaned_data.get('user_type')
if Event.objects.filter(user_type=user_type, date=date,
time_start__lt=time_start,
time_end__gt=time_start).exclude(pk=self.instance.pk).count():
raise forms.ValidationError("Overlapping with another event.")
Run Code Online (Sandbox Code Playgroud)
在将所有字段留空的情况下提交表单会导致a
ValueError:不能将None用作查询值.
如果我删除我的clean()方法,我将得到预期ValidationErrors的不填写必填字段 - 这是我所期望的clean()方法仍然存在.
知道什么可能导致这种情况发生吗?如果Django 在调用clean 之前没有检查所需的值,我会感到惊讶.
我正在使用ModelForm并尝试在Django forms.RadioSelect小部件上设置一个css类
class Meta:
model = models.MyModel
fields = ('rating',)
widgets = {
'rating': forms.RadioSelect(attrs={'class':'star'}),
}
Run Code Online (Sandbox Code Playgroud)
但是class ='star'不会在html中呈现.
我也试过用:
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
self.fields['rating'].widget.attrs['class'] = 'star'
Run Code Online (Sandbox Code Playgroud)
这也没用.我尝试使用forms.Textarea小部件做同样的事情,在那里我能够获得css类.
我在这里做错了什么或者RadioSelect不支持class属性(我希望类名应用于所有无线电输入)?
当我尝试创建一个 ModelForm 时
MyModelForm(instance=a_model_instance_)
Run Code Online (Sandbox Code Playgroud)
似乎 Django 阻止了表单__init__方法中初始模型字段的任何设置,例如:
def __init__(self, *args, **kwargs):
super(MyModelForm, self).__init__(*args, **kwargs)
if self.instance.pk:
if self.instance.my_field:
my_field = self.instance.my_field
else:
# show parent's field instead
my_field = self.instance.parent.my_field
self.fields['my_field'].initial = my_field
Run Code Online (Sandbox Code Playgroud)
__init__一旦表单绑定到实例,是否有任何理由为什么在表单的方法中初始化字段不再起作用?
我刚刚开始使用django-pagination,它包含在我用于其中一个项目的其他第三方应用程序中.
有没有人知道是否有可能用自定义版本替换django-pagination中的pagination.html模板而不必破解实际的应用程序?文档中没有提到任何内容,pagaination.html在templatetag(paginate())中进行了硬编码.我想知道是否有一种机制允许覆盖通过设置的模板
register.inclusion_tag('pagination/pagination.html', takes_context=True)(
paginate)
Run Code Online (Sandbox Code Playgroud)
从我自己的应用程序?
我有以下场景:
base.html文件:
{% block content %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
child.html:
{% extends 'base.html' %}
{% block content %}
<p>Overriding content</p>
{% endblock %}
{% block child_block %}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
child_of_child.html:
{% extends 'child.html' %}
{% block child_block %}
<p>Overriding child</p>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
在child.html中创建一个新的块child_block并让child_of_child.html扩展child.html并覆盖此块不起作用,直到我在base.html中包含child_block作为钩子.
是否无法在根模板中创建新的模板块/挂钩?如果是这样,有没有办法绕过它而不必在base.html中包含所有可能的钩子?