[Answered ]-The correct value is not saved in the database when I create an object in a Django project

1๐Ÿ‘

โœ…

Hi Simeon Todorov!

The problem seems to be in your template file add_organization.html

In the <form> tag. The two <input> fields are conflicting with the same name=name attributes.

<div class="flex flex-col mb-4">
  <label class="mb-2 text-lg text-gray-900" for="name">Organization Name</label> {{ organization_form.name.errors|striptags }}
  <input class="border py-2 px-3 text-grey-800 rounded-lg" type="text" name="name" id="name">
</div>
<div class="flex flex-col mb-4">
  <label class="mb-2 text-lg text-gray-900" for="email">Organization Owner email</label> {{ user_form.email.errors|striptags }}
  <input class="border py-2 px-3 text-grey-800 rounded-lg" type="text" name="email" id="email">
</div>
<div class="flex flex-col mb-4">
  <label class="mb-2 text-lg text-gray-900" for="name">Organization Admin Team Name</label> {{ team_form.name.errors|striptags }}
  <input class="border py-2 px-3 text-grey-800 rounded-lg" type="text" name="name" id="name">
</div>

Since they both have the name="name", when the form is submitted, the value of the Organization Admin Team Name is overwriting the value of the Organization Name.

Iโ€™d suggest to use different naming for both <input> tags name and id attributes.

For Organization Name:

<input name="organization_name" id="organization_name">

For Organization Admin Team Name:

<input name="team_name" id="team_name">

Then handle them in the backend accordingly!

Hope I helped you with this.

๐Ÿ‘คNomanAbid

0๐Ÿ‘

Thank you all for the quick and thorough replies. After I made the change from "name" to "organization_name" and the corresponding change in the Organization and Team forms, everything worked as it should.

forms.py

class AddOrganizationForm(forms.ModelForm):

custom_names = {'name': 'organization_name'}

def add_prefix(self, field_name):
    field_name = self.custom_names.get(field_name, field_name)
    return super(AddOrganizationForm, self).add_prefix(field_name)

class Meta:
    model = Organization
    fields = ['name']
๐Ÿ‘คSimeon Todorov

Leave a comment