1. Open forms.py
and validate the StockCreateForm
To prevent saving object with a blank item name
class StockCreateForm(forms.ModelForm):
class Meta:
model = Stock
fields = ['category', 'item_name', 'quantity']
def clean_category(self):
category = self.cleaned_data.get('category')
if not category:
raise forms.ValidationError('This field is required')
return category
def clean_item_name(self):
item_name = self.cleaned_data.get('item_name')
if not item_name:
raise forms.ValidationError('This field is required')
return item_name
2. Alerting and preventing the duplicate entry of computer name
for instance in Stock.objects.all():
if instance.category == category:
raise forms.ValidationError(str(category) + ' is already created')
return category
Never feel alone, its just that we’re ghost readers. Keep it coming, the tutorials are very educative
Thanks Samuel. Am glad you like the content
Hello Abdourahman, I kind of new with Django, I tried all kind of methods to prevent duplicated articles, however, they not working for me. any advice appreciated. thank you.
Hi Hassan, it’s normal to challenges. Please let me know more about your error.
Hi Arbad,
Firstly, thanks for the tutorial, it takes a lot of effort and insight to come up with such videos.
category has two validations viz., blank field and already existing one. This prevents from adding anything that is existing because the “existing” check will always say that this category exists. And we neither want nor can keep it blank. How do we get around this ?
Thanks!
Om
to be more clear about the query, let’s say that there is a category called “server” and I want to add to this existing category a new model of server say Dell2120 but I cannot add it because, I cannot keep the category blank because of the check and if I fill “server” to the category, it will say, the category already exists. How do we get around this ?
That was just an example on how to prevent duplicate data. The category field was not the best place to use that because you will need to have multiple items with the same category. It will fit the items field more than the category. I removed it in the subsequent videos. Let me know if you get it right.
Thank you Sir, I will go through the next videos and let you know. Your prompt responses are appreciated. Do you mind sharing your personal email id on omen.singh@gmail.com?
HI
am getting an error for indentation while am trying validation for item name.Can u please help me to solve this?
Make sure you use either a space bar or tab and maintain it for that code block. You can highlight the code block to see if the indentations are using the same marks (…. or —-). One of the marks is Spaces and the other Tabs.
pls, do not use disame database name as your variable like this
if instance.category == category: , its confusing for us.
or is it best practice to use the same name as your variable ?
Ok I will try to use different names. Thanks
hi , brother
how to display the validation text in red color below the validated input field
Use css to color the it. You can inspect the page and know which class name of id the error note uses
i am facing a unindent doest not match any outer identenation level
Highlight the area and see the tab differences. You can also check on a few of my videos. I can remember explaining how to easily solve it.
Hi I’m on step 17 and when I try to search items it gives me an error saying “name ‘header’ is not defined”. help
There is a variable called “header” in the view, remove it or just comment it
i have followed tutorial 17, defined clean_category() and clean_items and have successfully compiled the code, but the validation is not working, only the browser validation works for unfilled form fields, the red validationError is not coming and category which already existed are also getting inserted in the form
Check in the last few videos on this stock management system how I solve it. Remove the validation on the edit form
Check in the last few videos on the stock management system how i solve it
I am unable to get the form validation to work correctly. I am able to set blank=False in the model just fine and it works. When I set it back to true, and then implement the validation code, everything compiles fine and the server works fine. However when I submit the item gets added to the database without any validation error at all. This happens with checking for a blank field and also happens when trying to check for duplicates. Any help would be appreciated as I cant seem to find a solution. Ive compared my code with yours, and searched Django docs and all seem to show that I have it right.
send me the code on the community
I am unable to get the form validation to work like in video 17. I am able to set the model blank = false and this works fine. But when I implement the custom validation the code seems to work fine, the server starts fine but the validation never triggers. Everything gets saved to the database, blank or duplicates, does not matter. I have looked at Django docs, I have searched stackoverflow and nothing seems to fix the issue. I am unsure what is happening. I have followed your tutorial exactly
send me the code on the community
In this video, how the functions “clean_category” and “clean_item_name” are called. I mean when they are getting called
How the functions “clean_category” and “clean_item_name” are getting called.
they are Django built in i think