Crispy-forms InlineRadios 不显示我的模型状态

Crispy-forms InlineRadios don't show my model state

奇怪,我有一个带有 Charfield 的模型,它有一些可能的值

class Entry(models.Model):
    title = models.CharField(
        max_length=100,
        blank=True,
        null=True,
        help_text="We will refer to your entry by this title.")
    production_type = models.CharField(
        blank=False,
        null=True,
        max_length=32,
        default=None,
        choices=  (('Agency', 'Agency'),('Brand', 'Brand'),))

当我渲染一个简单的 ModelForm 时

class EntryForm(forms.ModelForm):
    class Meta:
        model = Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, request, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'title',
            'production_type'
        )

它为选择了正确状态的 production_type 字段呈现下拉列表。如果我如下添加 InlineRadios 小部件,则单选按钮在呈现时不会选择模型状态。我总是得到第一选择。表单提交正常。

class EntryForm(forms.ModelForm):
    class Meta:
        model = Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, request, *args, **kwargs):
        self.helper = FormHelper()
        self.helper.layout = Layout(
            'title',
             InlineRadios('production_type', css_class='production_type_holder')
        )

有什么方法可以 内联单选按钮行为并跟踪我的模型状态?

可能有一些错误:

  1. EntryForm 有行 model = Entry,而必须是 model = Payment ,如您描述的模型 Payment
  2. EntryForm.__init__中缺行super(EntryForm, self).__init__(*args, **kwargs)
  3. 你的 __init__ 声明包含冗余(?)参数 request 所以声明必须是

    def __init__(self, *args, **kwargs): #without request
    

请尝试此更改。

您的代码(在我的版本中,如下)在单选按钮的正常行为下工作正常,它通常会创建新的付款并打印现有的。

models.py 无变化

forms.py

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Field, Fieldset, ButtonHolder, Submit
from crispy_forms.bootstrap import InlineRadios

class EntryForm(forms.ModelForm):
    class Meta:
        model = Payment # !not Entry
        fields = [
            'title', 'production_type'
        ]
    def __init__(self, *args, **kwargs):  #!without request
        super(EntryForm, self).__init__(*args, **kwargs)  #this line present
        self.helper = FormHelper()

        self.helper.layout = Layout(
            'title',
            InlineRadios('production_type'),
            Submit('submit', 'Submit'), # i miss your submit button, so adding here
        )

views.py

from django.views.generic.edit import CreateView


# it creates new payment 
class PaymentCreate(CreateView):
    model = Payment
    form_class= EntryForm
    template_name='payment_form.html'
    success_url = '/payment/'

# this usual view just print form with already created instance
def PaymentView (request):
    form =  EntryForm(instance = Payment.objects.get(id=4))
    c = {'form': form}
    return render(request,'payment_form.html', c)

payment_form.html

{% load crispy_forms_tags %}
<html>
<head>
</head>
<body>

    {% csrf_token %}
    {% crispy form %}

</body>
</html>

urls.py:

...
#i made 2 views, one for 'usual' view, one for generic view
url(r'payment_view/', PaymentView), 
url(r'payment/', PaymentCreate.as_view()),
...

原来是虚惊一场。我的设计师从根本上改变了 css 的方式,打破了松脆的形式。我以为我已经隔离了更改,但是一旦我删除了所有生成的应用程序 CSS ,一切都运行良好。对不起,误报了。