17 – HOW TO VALIDATE AND PREVENT DUPLICATE DATA IN DJANGO – STOCK MANAGEMENT SYSTEM

Spread the love

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

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

28 Comments

  • 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 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.

    • 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 ?

  • 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

  • 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

  • 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.

  • 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

  • In this video, how the functions “clean_category” and “clean_item_name” are called. I mean when they are getting called

Leave a Reply

Your email address will not be published. Required fields are marked *