I'm trying to recreate a form with Inline using django and django-stuff. I already got it once but this one is not being easy!
我正在尝试用内联的django和django-stuff创建一个表单。我已经有过一次了,但这次不是那么容易!
I wish to create a form that can register a Client and its addresses, or something similar to this link here.
我希望创建一个表单,可以注册一个客户端和它的地址,或者类似于此链接的内容。
Example 1:Adding contacts
示例1:添加联系人
Example 2:Adding addresses
示例2:添加地址
I'm trying like this:
我试着像这样:
Forms.py
Forms.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', ),
),
)
Views.py
Views.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()
Models.py
Models.py
# from django.contrib.auth.models import User
from django.db import models
class Person(models.Model):
# class Meta:
# abstract = True
name = models.CharField('Nome',max_length=100)
birthday = models.DateField('Aniversário')
address1 = models.CharField('Endereço 1',max_length=100)
purchase_limit = models.DecimalField('Limite de compra',max_digits=15, decimal_places=2)
class Meta:
verbose_name_plural = 'pessoas'
verbose_name = 'pessoa'
def __str__(self):
return self.name
def get_child(self):
if hasattr(self, 'client'):
return self.client
elif hasattr(self, 'employee'):
return self.employee
else:
return None
def get_type(self):
if hasattr(self, 'client'):
return 'client'
elif hasattr(self, 'employee'):
return 'employee'
else:
return None
class Address(models.Model):
KINDS_CHOICES = (
('P', 'PRINCIPAL'),
('C', 'COBRANÇA'),
('E', 'ENTREGA'),
)
person = models.ForeignKey('Person')
kynd = models.CharField('Tipo', max_length=1, choices=KINDS_CHOICES)
public_place = models.CharField('Logradouro',max_length=150)
number = models.CharField('Número',max_length=150)
city = models.CharField('Cidade',max_length=150)
state = models.CharField('Estado',max_length=150)
zipcode = models.CharField('Cep',max_length=10)
country = models.CharField('País',max_length=150, choices=COUNTRY_CHOICES)
phone = models.CharField('Fone',max_length=50)
class Meta:
verbose_name_plural = 'endereços'
verbose_name = 'endereço'
def __str__(self):
return self.public_place
class Client(Person):
compra_sempre = models.BooleanField('Compra Sempre',default=False)
def save(self, *args, **kwargs):
super(Client, self).save(*args, **kwargs)
class Meta:
verbose_name = 'Cliente'
verbose_name_plural = 'Clientes'
class Employee(Person):
ctps = models.CharField('Carteira de Trabalho',max_length=25)
salary = models.DecimalField('Salário',max_digits=15, decimal_places=2)
def save(self, *args, **kwargs):
# self.operacao = CONTA_OPERACAO_DEBITO
super(Employee, self).save(*args, **kwargs)
class Meta:
verbose_name = 'Funcionário'
verbose_name_plural = 'Funcionários'
But djangosempre returns the error below:
但djangosempre返回的错误如下:
Can someone help me?
有人能帮助我吗?
1 个解决方案
#1
0
Django Material has no support for the django-extra-views inlines.
Django材料不支持Django - outview的inline。
With the django-material package, the related object could be handled just like a usual form field.
使用django-material包,可以像通常的表单字段一样处理相关的对象。
http://docs.viewflow.io/forms_formsets.html
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'),
)
#1
0
Django Material has no support for the django-extra-views inlines.
Django材料不支持Django - outview的inline。
With the django-material package, the related object could be handled just like a usual form field.
使用django-material包,可以像通常的表单字段一样处理相关的对象。
http://docs.viewflow.io/forms_formsets.html
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'),
)