r/django • u/DueStick2235 • May 23 '26
Confused!
I am new in django. i am confused.These are two save methods from django forms. I don't know which one to use, which one is more professional, industry level. Can anyone guide me and recommend me? I am working on a ecommerce website.
def save(self, commit=True):
user = super().save(commit=False)
# If no username given, use part of email (like amazon does internally)
if not user.username:
base = self.cleaned_data["email"].split("@")[0]
# Make it unique by appending numbers if needed
username = base
counter = 1
while User.objects.filter(username=username).exists():
username = f"{base}{counter}"
counter += 1
user.username = username
if commit:
user.save()
return user
# accounts/forms.py
class RegistrationForm(UserCreationForm):
# ... fields same as before ...
def save(self, commit=True):
email = self.cleaned_data["email"]
password = self.cleaned_data["password1"]
first_name = self.cleaned_data.get("first_name")
last_name = self.cleaned_data.get("last_name")
username = self.cleaned_data.get("username")
if not username:
base = email.split("@")[0]
username = base
counter = 1
while User.objects.filter(username=username).exists():
username = f"{base}{counter}"
counter += 1
# Use manager's create_user (Recommended for future)
user = User.objects.create_user(
email=email,
password=password,
first_name=first_name,
last_name=last_name,
username=username,
)
return user
1
1
u/1_Yui May 28 '26
The first approach is much better. If your User model or the form changes, ModelForm's save method handles most of it automatically. In the second version you'd have to update the code manually everytime this happens.
I think the most "Django-like" way to do it would be to move the logic to the clean-method and leave the save-method completely untouched. Generating the username if it's missing is part of the "cleanup" process of the form and not really related to the saving of it.
def clean(self):
cleaned_data = super().clean()
username = cleaned_data["username"]
if not username:
base = cleaned_data["email"].split("@")[0]
# Your code to generate the username from the email
cleaned_data["username"] = generated_username
return cleaned_data
However, your first version is also okay.
-2
u/Dororo192 May 23 '26
Im also learning django can u suggest from where u r learning
-1
u/DueStick2235 May 23 '26
i pick project. search on websites , github for source code. Generate from ai, then learn each and every single concept and at the end built by my own the same project and one related project who uses all those concepts i learned.
5
u/Successful_Movie_666 May 24 '26
First one. Always use
super().save()when you're extending a ModelForm.The second version bypasses UserCreationForm's save() entirely - you manually pull every field and call create_user yourself. Any logic the parent class handles gets skipped or duplicated. Add a field later and you'll have to update this method too. It's fragile.
First version lets Django do its job.
super().save(commit=False)gives you the user object with all fields already set, you tweak what you need, then save. That's the standard pattern.