I'm using the phone number MultiWidget provided by derek73. Question is: it puts three separate values in the POST - how should I recombine them into one value?
class USPhoneNumberMultiWidget(forms.MultiWidget):
"""
A Widget that splits US Phone number input into three <input type='text'> boxes.
"""
def __init__(self,attrs=None):
widgets = (
forms.TextInput(attrs={'size':'3','maxlength':'3', 'class':'phone'}),
forms.TextInput(attrs={'size':'3','maxlength':'3', 'class':'phone'}),
forms.TextInput(attrs={'size':'4','maxlength':'4', 'class':'phone'}),
)
super(USPhoneNumberMultiWidget, self).__init__(widgets, attrs)
def decompress(self, value):
if value:
return value.split('-')
return (None,None,None)
def value_from_datadict(self, data, files, name):
value = [u'',u'',u'']
# look for keys like name_1, get the index from the end
# and make a new list for the string replacement values
for d in filter(lambda x: x.startswith(name), data):
index = int(d[len(name)+1:])
value[index] = data[d]
if value[0] == value[1] == value[2] == u'':
return None
return u'%s-%s-%s' % tuple(value)
class UserForm(forms.ModelForm):
email = forms.EmailField(max_length=30,
widget=forms.TextInput(attrs={"class":"text"}))
first_name = forms.CharField(max_length=max_length_for_att(User, 'first_name'),
widget=forms.TextInput(attrs={"class":"text"}))
last_name = forms.CharField(max_length=max_length_for_att(User, 'last_name'),
widget=forms.TextInput(attrs={"class":"text"}))
phone = USPhoneNumberField(label="Phone",
widget=USPhoneNumberMultiWidget())
class Meta:
model = User
fields = ('email', 'first_name', 'last_name', 'phone', )
Look at the USPhoneNumberField's clean() method.
Make sure you are accessing the data from form.cleaned_data and not request.POST.
Related
I have a django view that generates a form depending on POST or GET. It works well with little data on the Student model but stagnates when the data gets big. Is there a remedy I can apply??
Here's the view
def My_View(request,pk):
if request.method == 'POST':
try:
form = NewIssueForm(request.POST,school= request.user.school,pk=pk,issuer = request.user)
if form.is_valid():
name = form.cleaned_data['borrower_id'].id
form.save(commit=True)
books = Books.objects.get(id=pk)
Books.Claimbook(books)
return redirect('view_books')
messages.success(request,f'Issued successfully')
except Exception as e:
messages.warning(request,f"{e}")
return redirect('view_books')
else:
form = NewIssueForm(school= request.user.school,pk=pk,issuer = request.user)
return render(request, 'new_issue.html', {'form': form})
Here's the form
class NewIssueForm(forms.ModelForm):
def __init__(self,*args, pk,school,issuer, **kwargs):
super(NewIssueForm, self).__init__(*args, **kwargs)
booqs = Books.objects.filter(school=school).get(id=pk)
self.fields['issuer'].initial = issuer
self.fields['borrower_id'].Student.objects.filter(school = school)
self.fields['book_id'].label = str(booqs.book_name) + " - " + str(booqs.reg_no)
self.fields['book_id'].initial = pk
class Meta:
model = Issue
fields = ['issuer','book_id','borrower_id','due_days']
widgets = {
'book_id':forms.TextInput(attrs={"class":'form-control','type':''}),
'issuer':forms.TextInput(attrs={"class":'form-control','type':'hidden'}),
'borrower_id': Select2Widget(attrs={'data-placeholder': 'Select Student','style':'width:100%','class':'form-control'}),
'due_days':forms.TextInput(attrs={"class":"form-control"}),
Student model
class Student(models.Model):
school = models.ForeignKey(School, on_delete=models.CASCADE)
name = models.CharField(max_length=200,help_text="The student's name in full")
now = datetime.datetime.now()
YEAR = [(str(a), str(a)) for a in range(now.year-5, now.year+1)]
year = models.CharField(max_length=4, choices = YEAR,help_text='The year the student is admitted in school')
student_id = models.CharField(max_length=40,help_text = "This is the student's admission number")
klass = models.ForeignKey(Klass,on_delete=models.CASCADE)
stream = models.ForeignKey(Stream,on_delete=models.CASCADE)
graduated = models.BooleanField(default=False,help_text = "Tick the box to mark the student as graduated")
prefect = models.BooleanField(default=False,help_text = "Tick the box to select the student as a prefect")
def __str__(self):
return "%s - %s - F%s %s"%(self.student_id,self.name,self.klass,self.stream)
class Meta:
verbose_name_plural = 'Students'
unique_together = ("school", "student_id",)
indexes = [models.Index(fields=['student_id']),
models.Index(fields=['name', ]),
The reason this happens is because when you render the Student, it will need to fetch the related Klass and Stream objects, and will make a query per Student object. This problem is called an N+1 problem since fetching n student requires one query to fetch all the Students, and then one query per Student for the Klasses, and one query per Student for the Streams.
You can select the data when you select the students with:
self.fields['borrower_id'].Student.objects.filter(
school=school
).select_related('klass', 'stream')
Depending on the __str__ implementations of other models, you might have to select data along with these models as well.
Can't guess your model but it seems like select_related may play a trick.
I would like to make two fields (repertoire and performer) required only if the user selects and submits a type="performance". This is my model:
class Event(models.Model):
EV_TYPE = (
('performance', 'performance'),
('other', 'other'),
)
title = models.CharField(max_length=200)
type = models.CharField(max_length=15, choices=EV_TYPE, default="performance")
repertoire = models.ManyToManyField(Work, blank=True)
performer = models.ManyToManyField(Profile, limit_choices_to={'role__type__contains': 'Performer'})
My form:
class EventForm(forms.ModelForm):
class Meta:
model = Event
fields = [
'type',
'submitted_by',
'title',
'repertoire',
]
My view:
def event_create_view(request):
if request.method == 'POST':
form_event = EventForm(
request.POST,
repertoire_required=(not (active_user.id == 17)), # if the user is NMS then the repertoire field is not compulsory
initial={'submitted_by' : active_user.profile.id}
)
form_venue = AddVenueForm()
form_profile = ProfileForm()
form_work = WorkFromEventForm()
if form_event.is_valid():
this_event = form_event.save()
return redirect('event-edit', id=this_event.id)
This is what I tried. In the above 'form_event.is_valid', I did:
if form_event.is_valid():
this_event = form_event.save(commit=False)
if this_event['type'] == 'performance' and this_event['performer'] and this_event['repertoire']:
this_event.save()
return redirect('event-edit', id=this_event.id)
This doesn't work though (I still get a 'this field is required' when I submit). How can I achieve what I want to do?
Addendum
This is also not working:
if
this_event['type'] == 'performance' and not this_event['performer'] or not this_event['repertoire']:
messages.error(request, form_event.errors)
else:
this_event.save()
You do that with a clean method on the form.
For example you might do;
def clean(self):
"""
Validate the form input
"""
cleaned_data = super().clean()
type = cleaned_data.get('type')
if type == 'performance':
repertoire = cleaned_data.get('repertoire')
performer = cleaned_data.get('performer')
if not repertoire:
self.add_error('repertoire', _("This field is required"))
if not performer:
self.add_error('performer', _("This field is required"))
return cleaned_data
The docs for form validation are here; https://docs.djangoproject.com/en/3.1/ref/forms/validation/
I have an update form with 4 fields to display
3 of them are related to a class to which the form is bound
the last field (country) is only for information and I would like that field to be display in first position
currently, it is displayed at the end of my form...
I tryed to use field_order but country field is ignored...
form.py
class ParametrageForm(forms.ModelForm):
def __init__(self, request, *args, **kwargs):
super(ParametrageForm, self).__init__(*args, **kwargs)
self.request = request
self.language = request.session.get('language')
self.user = request.user.id # id de l'utilisateur
self.user_pays = request.session.get('user_pays') # pays de l'utilisateur
self.user_site_type = request.session.get('user_site_type')
self.user_site = request.session.get('user_site')
instance = Parametrage.objects.get(asp_par_cle = kwargs['instance'].asp_par_cle)
SITE_CONCERNE = Site.option_list_sites(self.language)
if self.language == 'en':
country= Site.objects.get(sit_abr = instance.asp_par_loc).reg.pay.pay_nom_eng
elif self.language == 'fr':
country= Site.objects.get(sit_abr = instance.asp_par_loc).reg.pay.pay_nom_fra
else:
country= Site.objects.get(sit_abr = instance.asp_par_loc).reg.pay.pay_nom_eng
self.fields["country"] = forms.CharField(label = _("Country"),widget=forms.TextInput,initial=country, disabled=True)
self.fields["asp_par_loc"] = forms.ChoiceField(label = _("Site concerned by settings"), widget=forms.Select, choices=SITE_CONCERNE,)
self.fields["asp_par_ale"] = forms.IntegerField(label = _("Stock alert value for this site"), widget=forms.TextInput,)
self.fields["asp_par_con"] = forms.IntegerField(label = _("Stock confort value for this site"), widget=forms.TextInput,)
class Meta:
model = Parametrage
fields = ('asp_par_loc','asp_par_ale','asp_par_con',)
field_order = ['country','asp_par_loc','asp_par_ale','asp_par_con',]
Replace
self.fields["country"] = forms.CharField(label = _("Country"),widget=forms.TextInput,initial=country, disabled=True)
with
country = forms.CharField(label = _("Country"),widget=forms.TextInput,initial=country, disabled=True)
I found how to solve my issue:
I defined 'country' field outside of the init
and then filed_order works
but doing that, I do not have access to my initial country value, set in init (country= Site.objects.get(sit_abr = instance.asp_par_loc).reg.pay.pay_nom_eng)
I must improve my Python understanding...
I want to change ManyToManyField Name. user_groups is manytomanyfield. I tried to use ManyToManyRelatedField and also PrimaryKeyRelatedField but it is giving error. How can i change or with data type should i give like for character field i am giving CharField
class EmployeeProfileSerializer(serializers.ModelSerializer):
employee_id = serializers.CharField(source='user_employee_id')
payroll_id = serializers.CharField(source='user_payroll_id')
phone = serializers.CharField(source='user_phone')
hire_date = serializers.DateField(source='user_hire_date')
pay_rate = serializers.IntegerField(source='user_pay_rate')
salaried = serializers.CharField(source='user_salaried')
excempt = serializers.CharField(source='user_excempt')
state = serializers.CharField(source='user_state')
city = serializers.CharField(source='user_city')
zipcode = serializers.IntegerField(source='user_zipcode')
status = serializers.CharField(source='user_status')
class Meta:
model = UserProfile
fields = [
'employee_id',
'phone',
'payroll_id',
'hire_date',
'pay_rate',
'salaried',
'excempt',
'state',
'city',
'zipcode',
'status',
'user_groups',
]
You can use SerializerMethodField:
class EmployeeProfileSerializer(serializers.ModelSerializer):
...
your_custom_name = SerializerMethodField()
class Meta:
model = UserProfile
fields = ['your_custom_name ', ...]
def get_your_custom_name(self, obj):
# Return ids:
return list(obj.user_groups.all().values_list('pk', flat=True))
# Or using a serializer:
return MyUserGroupSerializer(obj.user_groups.all(), many=True).data
For create and update you have to override the create and update method to assign the new field:
class EmployeeProfileSerializer(serializers.ModelSerializer):
...
your_custom_name = IntegerField()
class Meta:
model = UserProfile
fields = ['your_custom_name', ...]
# If you need validation
def validate_your_custom_name(self, value):
if value:
if int(value) > 5:
return value
return None
def create(self, validated_data):
# Get the data for your new field
my_costum_data = validated_data.get('your_custom_name')
# Do something with it
profile_obj = UserProfile.objects.create(...)
if my_costum_data:
user_group = UserGroupModel.objects.get(pk=int(my_costum_data))
profile_obj.user_groups.add(user_group)
def update(self, instance, validated_data):
# Same as create()
...
Is it possible for an inlineformset_factory to take in a ModelForm as well as a model. When I try to run this I get an error message 'NoneType' object is not iterable.
Please help, I've spent an entire day trying to figure this out. Thanks.
Code:
Model.py
class FilterForm(ModelForm):
firstFilter = forms.BooleanField(label='First Filter', initial=False, required=False)
class Meta:
model = Filter
exclude = ('order')
class Controller(models.Model):
protocol = models.CharField('Protocol',max_length=64, choices=PROTOCOLS, default='http')
server = models.CharField('Server', max_length=64, choices=SERVERS, default='127.0.0.1')
name = models.CharField('Name', max_length=64)
def __unicode__(self):
return self.protocol + '://' + self.server + '/' + self.name
view.py
def controller_details(request, object_id):
controller = Controller.objects.get(pk=object_id)
controllerURI = controller.protocol + '://' + controller.server + '/' + controller.name
FilterFormSet = inlineformset_factory(Controller, FilterForm, extra=5)
if request.method == 'POST':
formset = FilterFormSet(request.POST, request.FILES, instance=controller)
if formset.is_valid():
filters = []
# Save all the filters into a list
forms = formset.cleaned_data
for form in forms:
if form:
protocol = form['protocol']
server = form['server']
name = form['name']
targetURI = form['targetURI']
filterType = form['filterType']
firstFilter = form['firstFilter']
if firstFilter == True:
aFilter = Filter(controller=controller, protocol=protocol, server=server, name=name, targetURI=targetURI, filterType=filterType, order=0)
else:
aFilter = Filter(controller=controller, protocol=protocol, server=server, name=name, targetURI=targetURI, filterType=filterType, order=-1)
filters.append(aFilter)
# Find the first filter in the list of filters
for index, aFilter in enumerate(filters):
if aFilter.order == 0:
break
if filters[index].targetURI:
test = "yes"
else:
for aFilter in filters:
aFilter.save()
else:
formset = FilterFormSet(instance=controller)
return render_to_response('controller_details.html', {'formset':formset, 'controllerURI':controllerURI}, context_instance=RequestContext(request))
UPDATE: If you intended to create a FormSet with Controller and Filter models where Filter holds a FK to the Controller, you need:
FilterFormSet = inlineformset_factory(Controller, Filter, form=FilterForm)
Note that in your code above, you're only passing the the Controller model class, which caused some confusion.