Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:31:02+00:00 2026-06-08T04:31:02+00:00

I am trying to create a custom cleaning method which look in the db

  • 0

I am trying to create a custom cleaning method which look in the db if the value of one specific data exists already and if yes raises an error.
I’m using a model form of a class (subsystem) who is inheriting from an other class (project).
I want to check if the sybsystem already exists or not when i try to add a new one in a form.

I get project name in my view function.

class SubsytemForm(forms.ModelForm):  

    class Meta:
        model = Subsystem
        exclude = ('project_name')

    def clean(self,project_name):

        cleaned_data = super(SubsytemForm, self).clean(self,project_name)
        form_subsystem_name = cleaned_data.get("subsystem_name")

        Subsystem.objects.filter(project__project_name=project_name)
        subsystem_objects=Subsystem.objects.filter(project__project_name=project_name)
        nb_subsystem = subsystem_objects.count()

        for i in range (nb_subsystem):
            if (subsystem_objects[i].subsystem_name==form_subsystem_name):
                msg = u"Subsystem already existing"
                self._errors["subsystem_name"] = self.error_class([msg])

            # These fields are no longer valid. Remove them from the
            # cleaned data.
            del cleaned_data["subsystem_name"]
        return cleaned_data

My view function :

def addform(request,project_name):
    if form.is_valid(): 
        form=form.save(commit=False)
        form.project_id=Project.objects.get(project_name=project_name).id 
        form.clean(form,project_name)
        form.save()

This is not working and i don’t know how to do.
I have the error : clean() takes exactly 2 arguments (1 given)

My model :

class Project(models.Model):
project_name = models.CharField("Project name", max_length=20)

Class Subsystem(models.Model):
subsystem_name = models.Charfield("Subsystem name", max_length=20)
projects = models.ForeignKey(Project)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:31:04+00:00Added an answer on June 8, 2026 at 4:31 am

    There are quite a few things wrong with this code.

    Firstly, you’re not supposed to call clean explicitly. Django does it for you automatically when you call form.is_valid(). And because it’s done automatically, you can’t pass extra arguments. You need to pass the argument in when you instantiate the form, and keep it as an instance variable which your clean code can reference.

    Secondly, the code is actually only validating a single field. So it should be done in a specific clean_fieldname method – ie clean_subsystem_name. That avoids the need for mucking about with _errors and deleting the unwanted data at the end.

    Thirdly, if you ever find yourself getting a count of something, iterating through a range, then using that index to point back into the original list, you’re doing it wrong. In Python, you should always iterate through the actual thing – in this case, the queryset – that you’re interested in. However, in this case that is irrelevant anyway as you should query for the actual name directly in the database and check if it exists, rather than iterating through checking for matches.

    So, putting it all together:

    class SubsytemForm(forms.ModelForm):  
    
        class Meta:
            model = Subsystem
            exclude = ('project_name')
    
        def __init__(self, *args, **kwargs):
            self.project_name = kwargs.pop('project_name', None)
            super(SubsystemForm, self).__init__(*args, **kwargs)
    
        def clean_subsystem_name(self):
            form_subsystem_name = self.cleaned_data.get("subsystem_name")
    
            existing = Subsystem.objects.filter(
                           project__project_name=self.project_name,
                           subsytem_name=form_subsystem_name
                       ).exists()
    
            if existing:
                raise forms.ValidationError(u"Subsystem already existing")
    
            return form_subsystem_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create specific custom filter effects on image for iOS. So far,
I'm trying to create a custom validation method in JQuery and I can't manage
I am trying to create a Custom Attribute for a ServiceStack Service with which
I'm trying to create custom attributes to my button but I dont know which
I am trying to create a custom event which listens and responses to the
I am trying to create custom buttons, and I created one in photoshop and
Im trying to create a custom version of the RequiredAttribute to replace the built
I'm trying to create a custom TabItem that is dynamically added to a TabControl
I am trying to create a custom User library in CodeIgniter. Inside this library
I'm trying to create a custom SELECT query to find my Personal Best races

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.