changed forms to modelforms

This commit is contained in:
maxime 2019-06-08 13:38:59 +02:00
parent a4e56bf94e
commit 593b0dcfe6
2 changed files with 72 additions and 9 deletions

View File

@ -1,22 +1,55 @@
from django.forms import Form, CharField, IntegerField, DecimalField, \ from django.forms import Form, ModelForm, \
BooleanField, EmailField, DateTimeField, ImageField, formset_factory CharField, IntegerField, DecimalField, \
BooleanField, EmailField, DateTimeField, ImageField, \
formset_factory
from django_countries.fields import CountryField from django_countries.fields import CountryField
from phonenumber_field.formfields import PhoneNumberField from phonenumber_field.formfields import PhoneNumberField
from .models import *
class PersonForm(Form): class PersonForm(Form):
class Meta:
model = Person
fields = [
'name',
'surname',
'nationality',
'email',
'phone'
]
"""
name = CharField(label='Name', max_length=50) name = CharField(label='Name', max_length=50)
surname = CharField(label='Surname', max_length=50) surname = CharField(label='Surname', max_length=50)
nationality = CountryField().formfield(label='Nationality') nationality = CountryField().formfield(label='Nationality')
email = EmailField(label='E-Mail') email = EmailField(label='E-Mail')
phone = PhoneNumberField(label='Phone Number') phone = PhoneNumberField(label='Phone Number')
"""
class AddressForm(Form): class AddressForm(Form):
address = CharField(label='Name', max_length=200) class Meta:
zip_code = CharField(label='ZipCode', max_length=10) model = Address
city = CharField(label='City', max_length=200)
country = CountryField().formfield(label='Country')
class DockForm(Form): fields = [
'address',
'zip_code',
'city',
'country'
]
class DockForm(ModelForm):
class Meta:
model = Dock
fields = [
'name',
'length',
'width',
'depth_min',
'depth_max']
"""
name = CharField(label='Name', max_length=10) name = CharField(label='Name', max_length=10)
length = DecimalField( length = DecimalField(
label='Length', min_value=0, label='Length', min_value=0,
@ -34,15 +67,36 @@ class DockForm(Form):
label='Depth (max.)', min_value=0, label='Depth (max.)', min_value=0,
max_digits=7, max_digits=7,
decimal_places=2) decimal_places=2)
"""
DockFormSet = formset_factory(DockForm) DockFormSet = formset_factory(DockForm)
class PortForm(Form): class PortForm(ModelForm):
class Meta:
model = Port
fields = [
'name',
'address',
'company']
"""
name = CharField(label='Name', max_length=50) name = CharField(label='Name', max_length=50)
address = CharField(label='Address', max_length=50) address = CharField(label='Address', max_length=50)
company = CharField(label='Company', max_length=50) company = CharField(label='Company', max_length=50)
"""
class PlugForm(Form): class PlugForm(Form):
class Meta:
model = Plug
fields = [
'name',
'amperage',
'voltage',
]
"""
name = CharField(label='Name', max_length=10) name = CharField(label='Name', max_length=10)
amperage = DecimalField( amperage = DecimalField(
label='Amperage', label='Amperage',
@ -54,10 +108,19 @@ class PlugForm(Form):
min_value=0, min_value=0,
max_digits=3, max_digits=3,
decimal_places=0) decimal_places=0)
"""
PlugFormSet = formset_factory(PlugForm) PlugFormSet = formset_factory(PlugForm)
class TapForm(Form): class TapForm(Form):
class Meta:
model = Tap
fields = [
'name']
"""
name = CharField(label='Name', max_length=10) name = CharField(label='Name', max_length=10)
"""
TapFormSet = formset_factory(TapForm) TapFormSet = formset_factory(TapForm)

View File

@ -22,7 +22,7 @@ def add_port(request):
dock_forms = DockFormSet(request.POST) dock_forms = DockFormSet(request.POST)
dock_forms_data = dock_forms.save(commit=False) dock_forms_data = dock_forms.save(commit=False)
for dock_data in dock_forms_data: for dock_data in dock_forms_data:
pass pprint(dock_data)
plug_forms = PlugFormSet(request.POST) plug_forms = PlugFormSet(request.POST)
plug_forms_data = plug_forms.save(commit=False) plug_forms_data = plug_forms.save(commit=False)