class CapaForm(forms.Form):
capa = forms.CharField(required=False)
Upon form submission I want to check the format of the capa field. I want to require the user to enter the capa format correctly as 6 numbers, a dash, and two numbers. (######-##)
def search(self):
capa = self.cleaned_data.get('capa', None)
if ("\d{6}\-\d{2}" or None) not in capa:
raise forms.ValidationError("CAPA format needs to be ######-##!")
It's currently not letting me submit a correctly formatted capa and throws the ValidationError. I think the problem is I'm trying to compare a regular expression to an object. How can I check the format of the 'capa' the user tries to submit?
*********UPDATE
Everything is working now EXCEPT when I type the wrong format in the CAPA field. I get the error The view incidents.views.index didn't return an HttpResponse object. It returned None instead. Is this related to the changes I made?
from django.core.validators import RegexValidator
my_validator = RegexValidator("\d{6}\-\d{2}", "CAPA format needs to be ######-##.")
class CapaForm(forms.Form):
capa = forms.CharField(
label="CAPA",
required=False, # Note: validators are not run against empty fields
validators=[my_validator]
)
def search(self):
capa = self.cleaned_data.get('capa', None)
query = Incident.objects.all()
if capa is not '':
query = query.filter(capa=capa)
return(query)
First you need a regex validator:
Django validators / regex validator
Then, add it into the validator list of your field:
using validators in forms
Simple example below:
from django.core.validators import RegexValidator
my_validator = RegexValidator(r"A", "Your string should contain letter A in it.")
class MyForm(forms.Form):
subject = forms.CharField(
label="Test field",
required=True, # Note: validators are not run against empty fields
validators=[my_validator]
)
you could also ask from both part in your form, it would be cleaner for the user :
class CapaForm(forms.Form):
capa1 = forms.IntegerField(max_value=9999, required=False)
capa2 = forms.IntegerField(max_value=99, required=False)
and then just join them in your view :
capa = self.cleaned_data.get('capa1', None) + '-' + self.cleaned_data.get('capa2', None)
You can also use RegexField. It's the same as CharField but with additional argument regex. Under the hood it uses validators.
Example:
class MyForm(forms.Form):
field1 = forms.RegexField(regex=re.compile(r'\d{6}\-\d{2}'))
Regex validator does not work for me in Django 2.2
Step to set up custom validation for a field value:
define the validation function:
def number_code_validator(value):
if not re.compile(r'^\d{10}$').match(value):
raise ValidationError('Enter Number Correctly')
In the form add the defined function to validators array of the field:
number= forms.CharField(label="Number",
widget=TextInput(attrs={'type': 'number'}),
validators=[number_code_validator])
Related
I'm really confused how to use placeholders. I'm using model that has a couple of fields of data and I want to have a place holder telling he user what to enter. So placeholder sounds like a good way to put this info into the text field. I'm using Crispy forms and fields come from the model.
Here is my forms.py just don't know how to incorporate placeholders here if its even here where I need to do it.
from django import forms
from .models import MRReg
class SignUpForm(forms.ModelForm):
class Meta:
model = MRReg
fields = ['SiteID','HeliumValue']
def clean_HeliumValue(self):
HeliumValue = self.cleaned_data.get('HeliumValue')
#HeliumValue_base, provider = HeliumValue.split("#")
#domain, extension = provider.split('.')
#if not extension == "edu":
#raise forms.ValidationError("Please use a valid .edu address")
return HeliumValue
#print (self.cleaned_data)
def clean_SiteID(self):
SiteID = self.cleaned_data.get('SiteID')
raise forms.ValidationError("Please enter a valid SiteID")
return SiteID
If you use forms widgets like forms.CharField you can pass this to it, for example forms.CharField()
forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'my-placeholder'}))
I have created a simple form in which the user is specifying the name of a (Bloomberg)Ticker. In a second field the user is entering an address of a webpage. Of course, she/he can manually correct it but I would like to offer him/her a button such that the app suggests the name of the page based on the content of first Ticker field, e.g. the user enters "CARMPAT FP Equity" and then the app suggests correctly
https://www.bloomberg.com/quote/CARMPAT:FP
# forms.py
from django.forms import ModelForm, ValidationError
from pybloomberg.bloomberg import valid_ticker
from .models import Symbol
class SymbolAdminForm(ModelForm):
# Have a button next to address field...
class Meta:
model = Symbol
fields = '__all__'
def clean(self):
cleaned_data = self.cleaned_data
ticker = cleaned_data.get('bloomberg_symbol')
return cleaned_data
def react(self):
# extract the content for the bloomberg_symbol field
address = ...
# manipulate the content for the address field
self.... = address
If you want the suggestion to be applied automatically after the form is posted, you can move your logic to the clean() method and you will need to allow the address field to be empty.
def clean(self):
cleaned_data = self.cleaned_data
ticker = cleaned_data.get('bloomberg_symbol')
cleaned_data['address'] = ticker_name_to_address()
return cleaned_data
If you want the suggestion to be displayed to the users when they enter the name, you will have no choice but to use JavaScript.
I am trying to create a model to store an hashtag.
The validator doesn't seem to be working, making the field accept all inputs, and I can't find the solution.
Here is my model:
class Hashtags(models.Model):
hashtag_validator = RegexValidator(r'^[#](\w+)$', "Hashtag doesn't comply.")
hashtag_id = models.AutoField(primary_key=True)
hashtag_text = models.CharField(max_length=100, validators=[hashtag_validator], unique=True)
def get_id(self):
return self.hashtag_id
def get_text(self):
return self.hashtag_text
You can alter it to the below given code to see it working
hashtag_validator = CharField(
max_length=50,
required=True, #if you want that field to be mandatory
validators=[
RegexValidator(
regex='^[#](\w+)$',
message='Hashtag doesnt comply',
),
]
)
Hope that helps!!
If that is causing problem you can try writing your own validator
from django.core.exceptions import ValidationError
import re
def validate_hash(value):
reg = re.compile('^[#](\w+)$')
if not reg.match(value) :
raise ValidationError(u'%s hashtag doesnot comply' % value)
and change your model field to
hashtag_validator = models.Charfield(validators=[validate_hash])
Very late to the party so I doubt that this is still a problem for OP, but I will leave this here just for posterity and people that happen to come across this post. Probably you are instantiating and saving an object directly, e.g Hashtags(hashtag_text='invalid-tag').save(). This will not call the validators. The validators are only called when full_clean or clean is called, which is only done automatically if you go through a ModelForm. If you instantiate objects manually, either through the constructor or the object collection Hashtags.objects.create the validators will not be called.
In addition to S.Ali answer:
based on example from here
def uncvalidator(value):
"""Custom UNC path validator"""
import re
from django.utils.translation import gettext_lazy as _
UNC_REGEX = r'^local.company/some/share'
regex = re.compile(UNC_REGEX, re.IGNORECASE)
if not regex.match(value):
raise ValidationError(
_('Entered path %(value)s is incorrect.'),
params={'value': value},
)
unc = models.CharField(
validators=[uncvalidator],
)
I'm trying to learn Django and I've ran into some confusing points. I'm currently having trouble creating a movie using a form. The idea of the form is to give the user any field he'd like to fill out. Any field that the user fills out will be updated in its respective sql table (empty fields will be ignored). But, the form keeps giving me the error "Enter a list of values" when I submit the form. To address this, I thought stuffing the data from the form into a list and then returning that list would solve this.
The first idea was to override the clean() in my ModelForm. However, because the form fails the is_valid() check in my views, the cleaned_data variable in clean() doesn't contain anything. Next, I tried to override the to_python(). However, to_python() doesn't seem to be called.
If I put __metaclass__ = models.SubfieldBase in the respective model, I receive the runtime error
"TypeError: Error when calling the
metaclass bases
metaclass conflict: the metaclass of a derived class must be a
(non-strict) subclass of the
metaclasses of all its bases"
My approach doesn't seem to work. I'm not sure how to get around the 'Enter a list of values" error! Any advice?
Here is the relevant code (updated):
models.py
""" Idea:
A movie consists of many equipments, actors, and lighting techniques. It also has a rank for the particular movie, as well as a title.
A Theater consists of many movies.
A nation consists of many theaters.
"""
from django.db import models
from django.contrib.auth.models import User
class EquipmentModel(models.Model):
equip = models.CharField(max_length=20)
# user = models.ForeignKey(User)
class ActorModel(models.Model):
actor = models.CharField(max_length=20)
# user = models.ForeignKey(User)
class LightModel(models.Model):
light = models.CharField(max_length=20)
# user = models.ForeignKey(User)
class MovieModel(models.Model):
# __metaclass__ = models.SubfieldBase
rank = models.DecimalField(max_digits=5000, decimal_places=3)
title = models.CharField(max_length=20)
equipments = models.ManyToManyField(EquipmentModel, blank=True, null=True)
actors = models.ManyToManyField(ActorModel, blank=True, null=True)
lights = models.ManyToManyField(LightModel, blank=True, null=True)
class TheaterModel(models.Model):
movies = models.ForeignKey(MovieModel)
class NationModel(models.Model):
theaters = models.ForeignKey(TheaterModel)
=====================================
forms.py
"""
These Modelforms tie in the models from models.py
Users will be able to write to any of the fields in MovieModel when creating a movie.
Users may leave any field blank (empty fields should be ignored, ie: no updates to database).
"""
from django import forms
from models import MovieModel
from django.forms.widgets import Textarea
class MovieModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MovieModelForm, self).__init__(*args, **kwargs)
self.fields["actors"].widget = Textarea()
self.fields["equipments"].widget = Textarea()
self.fields["lights"].widget = Textarea()
def clean_actors(self):
data = self.cleaned_data.get('actors')
print 'cleaning actors'
return [data]
class Meta:
model = MovieModel
=============================================
views.py
""" This will display the form used to create a MovieModel """
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import MovieModelForm
def add_movie(request):
if request.method == "POST":
form = MovieModelForm(request.POST)
if form.is_valid():
new_moviemodel = form.save()
return HttpResponseRedirect('/data/')
else:
form = MovieModelForm()
return render_to_response('add_movie_form.html', {form:form,}, context_instance=RequestContext(request))
The probable problem is that the list of values provided in the text area can not be normalized into a list of Models.
See the ModelMultipleChoiceField documentation.
The field is expecting a list of valid IDs, but is probably receiving a list of text values, which django has no way of converting to the actual model instances. The to_python will be failing within the form field, not within the form itself. Therefore, the values never even reach the form.
Is there something wrong with using the built in ModelMultipleChoiceField? It will provide the easiest approach, but will require your users to scan a list of available actors (I'm using the actors field as the example here).
Before I show an example of how I'd attempt to do what you want, I must ask; how do you want to handle actors that have been entered that don't yet exist in your database? You can either create them if they exist, or you can fail. You need to make a decision on this.
# only showing the actor example, you can use something like this for other fields too
class MovieModelForm(forms.ModelForm):
actors_list = fields.CharField(required=False, widget=forms.Textarea())
class Meta:
model = MovieModel
exclude = ('actors',)
def clean_actors_list(self):
data = self.cleaned_data
actors_list = data.get('actors_list', None)
if actors_list is not None:
for actor_name in actors_list.split(','):
try:
actor = Actor.objects.get(actor=actor_name)
except Actor.DoesNotExist:
if FAIL_ON_NOT_EXIST: # decide if you want this behaviour or to create it
raise forms.ValidationError('Actor %s does not exist' % actor_name)
else: # create it if it doesnt exist
Actor(actor=actor_name).save()
return actors_list
def save(self, commit=True):
mminstance = super(MovieModelForm, self).save(commit=commit)
actors_list = self.cleaned_data.get('actors_list', None)
if actors_list is not None:
for actor_name in actors_list.split(","):
actor = Actor.objects.get(actor=actor_name)
mminstance.actors.add(actor)
mminstance.save()
return mminstance
The above is all untested code, but something approaching this should work if you really want to use a Textarea for a ModelMultipleChoiceField. If you do go down this route, and you discover errors in my code above, please either edit my answer, or provide a comment so I can. Good luck.
Edit:
The other option is to create a field that understands a comma separated list of values, but behaves in a similar way to ModelMultipleChoiceField. Looking at the source code for ModelMultipleChoiceField, it inhertis from ModelChoiceField, which DOES allow you to define which value on the model is used to normalize.
## removed code because it's no longer relevant. See Last Edit ##
Edit:
Wow, I really should have checked the django trac to see if this was already fixed. It is. See the following ticket for information. Essentially, they've done the same thing I have. They've made ModelMutipleChoiceField respect the to_field_name argument. This is only applicable for django 1.3!
The problem is, the regular ModelMultipleChoiceField will see the comma separated string, and fail because it isn't a List or Tuple. So, our job becomes a little more difficult, because we have to change the string to a list or tuple, before the regular clean method can run.
class ModelCommaSeparatedChoiceField(ModelMultipleChoiceField):
widget = Textarea
def clean(self, value):
if value is not None:
value = [item.strip() for item in value.split(",")] # remove padding
return super(ModelCommaSeparatedChoiceField, self).clean(value)
So, now your form should look like this:
class MovieModelForm(forms.ModelForm):
actors = ModelCommaSeparatedChoiceField(
required=False,
queryset=Actor.objects.filter(),
to_field_name='actor')
equipments = ModelCommaSeparatedChoiceField(
required=False,
queryset=Equipment.objects.filter(),
to_field_name='equip')
lights = ModelCommaSeparatedChoiceField(
required=False,
queryset=Light.objects.filter(),
to_field_name='light')
class Meta:
model = MovieModel
to_python AFAIK is a method for fields, not forms.
clean() occurs after individual field cleaning, so your ModelMultipleChoiceFields clean() methods are raising validation errors and thus cleaned_data does not contain anything.
You haven't provided examples for what kind of data is being input, but the answer lies in form field cleaning.
http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute
You need to write validation specific to that field that either returns the correct data in the format your field is expecting, or raises a ValidationError so your view can re-render the form with error messages.
update: You're probably missing the ModelForm __init__ -- see if that fixes it.
class MovieModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(MovieModelForm, self).__init__(*args, **kwargs)
self.fields["actors"].widget = Textarea()
def clean_actors(self):
data = self.cleaned_data.get('actors')
# validate incoming data. Convert the raw incoming string
# to a list of ids this field is expecting.
# if invalid, raise forms.ValidationError("Error MSG")
return data.split(',') # just an example if data was '1,3,4'
Please see the code below. Basically, when the user creates an object of this class, they need to specify the value_type. If value_type==2 (percentage), then percentage_calculated_on (which is a CheckboxSelectMultiple on the form/template side needs to have one or more items checked. The model validation isn't allowing me to validate like I'm trying to -- it basically throws an exception that tells me that the instance needs to have a primary key value before a many-to-many relationship can be used. But I need to first validate the object before saving it. I have tried this validation on the form (modelform) side (using the form's clean method), but the same thing happens there too.
How do I go about achieving this validation?
INHERENT_TYPE_CHOICES = ((1, 'Payable'), (2, 'Deductible'))
VALUE_TYPE_CHOICES = ((1, 'Amount'), (2, 'Percentage'))
class Payable(models.Model):
name = models.CharField()
short_name = models.CharField()
inherent_type = models.PositiveSmallIntegerField(choices=INHERENT_TYPE_CHOICES)
value = models.DecimalField(max_digits=12,decimal_places=2)
value_type = models.PositiveSmallIntegerField(choices=VALUE_TYPE_CHOICES)
percentage_calculated_on = models.ManyToManyField('self', symmetrical=False)
def clean(self):
from django.core.exceptions import ValidationError
if self.value_type == 2 and not self.percentage_calculated_on:
raise ValidationError("If this is a percentage, please specify on what payables/deductibles this percentage should be calculated on.")
I tested out your code in one of my projects' admin app. I was able to perform the validation you required by using a custom ModelForm. See below.
# forms.py
class MyPayableForm(forms.ModelForm):
class Meta:
model = Payable
def clean(self):
super(MyPayableForm, self).clean() # Thanks, #chefsmart
value_type = self.cleaned_data.get('value_type', None)
percentage_calculated_on = self.cleaned_data.get(
'percentage_calculated_on', None)
if value_type == 2 and not percentage_calculated_on:
message = "Please specify on what payables/deductibles ..."
raise forms.ValidationError(message)
return self.cleaned_data
# admin.py
class PayableAdmin(admin.ModelAdmin):
form = MyPayableForm
admin.site.register(Payable, PayableAdmin)
The Admin app uses the SelectMultiple widget (rather than CheckboxSelectMultiple as you do) to represent many to many relationships. I believe this shouldn't matter though.