我正在尝试使用django和django-stuff重新创建一个带有Inline的表单。 我已经得到过一次,但这个并不容易!
我希望创建一个可以注册客户端及其地址的表单,或者类似于此链接的内容。
示例1:Adding contacts
示例2:Adding addresses
我这样想:
Forms.py
long long
Views.py
from django import forms
from material import Layout, Row, Fieldset
from .models import Client
class Address(forms.Form):# TA FEITO
public_place = forms.CharField(label='Logradouro')
number = forms.CharField(label='Número')
city = forms.CharField(label='Cidade')
state = forms.CharField(label='Estado')
zipcode = forms.CharField(label='Cep')
country = forms.CharField(label='País')
phone = forms.CharField(label='Fone')
class Meta:
verbose_name_plural = 'endereços'
verbose_name = 'endereço'
def __str__(self):
return self.profissao
class ClientModelForm(forms.ModelForm):
class Meta:
model = Client
fields = '__all__'
layout = Layout(
Fieldset("Client",
Row('name', ),
Row('birthday','purchase_limit'),
Row('address1', ),
Row('compra_sempre', ),
),
)
的 Models.py
import extra_views
from braces.views import LoginRequiredMixin
from extra_views import CreateWithInlinesView, NamedFormsetsMixin
from material import LayoutMixin, Fieldset
from material.admin.base import Inline
from danibraz.persons.forms import *
from .models import Address
class ItemInline(extra_views.InlineFormSet):
model = Address
fields = ['kynd', 'public_place','number','city','state','zipcode','country','phone']#campos do endereço
extra = 1# Define aquantidade de linhas a apresentar.
class NewClientsView(LoginRequiredMixin,LayoutMixin,
NamedFormsetsMixin,
CreateWithInlinesView):
title = "Inclua um cliente"
model = Client
#print('Chegou na linha 334')
layout = Layout(
Fieldset("Inclua um cliente",
Row('name', ),
Row('birthday','purchase_limit'),
Row('address1', ),
Row('compra_sempre', ),
),
Inline('Endereços', ItemInline),
)
#print('Chegou na linha 340')
def forms_valid(self, form, inlines):
new = form.save(commit=False)
new.save()
form.save_m2m()
return super(NewClientsView, self).forms_valid(form, inlines)
def get_success_url(self):
return self.object.get_absolute_url()
但是djangosempre会返回以下错误:
有人可以帮助我吗?
答案 0 :(得分:0)
Django Material不支持django-extra-views内联。
使用django-material包,可以像通常的表单字段一样处理相关对象。
http://docs.viewflow.io/forms_formsets.html
class AddressForm(forms.Form):
line_1 = forms.CharField(max_length=250)
line_2 = forms.CharField(max_length=250)
state = forms.CharField(max_length=100)
city = forms.CharField(max_length=100)
zipcode = forms.CharField(max_length=10)
layout = Layout(
'line_1',
'line_2',
'state',
Row('city', 'zipcode'),
)
AddressFormSet = formset_factory(AddressForm, extra=3, can_delete=True)
class SignupForm(Form):
username = forms.CharField(max_length=50)
first_name = forms.CharField(max_length=250)
last_name = forms.CharField(max_length=250)
addresses = FormSetField(formset_class=AddressFormSet)
layout = Layout(
'username',
Row('first_name', 'last_name'),
'emails',
Stacked(1, 'addresses'),
)