में एक उपयोगकर्ता जोड़ें फॉर्म बनाना I सिंगल फ़ॉर्म जो व्यवस्थापक को विस्तारित प्रोफ़ाइल के साथ एक नया उपयोगकर्ता बनाने की क्षमता देता है। कृपया ध्यान दें कि, मैं व्यवस्थापक और पंजीकरण ऐप्स का उपयोग नहीं करना चाहता हूं। मैंने उपयोगकर्ता को उपयोगकर्ता प्रोफाइल मॉडल के साथ बढ़ा दिया है। मैंने उपयोगकर्ता प्रोफ़ाइल को विस्तारित करने से संबंधित सभी दस्तावेज पढ़े हैं। लेकिन, मैं वास्तव में नहीं जानता कि इन जानकारी को कैसे सहेजना है।Django
class CreateUserForm(forms.Form):
username = forms.CharField(max_length=30)
first_name = forms.CharField()
last_name = forms.CharField()
password1=forms.CharField(max_length=30,widget=forms.PasswordInput()) #render_value=False
password2=forms.CharField(max_length=30,widget=forms.PasswordInput())
email=forms.EmailField(required=False)
title = forms.ChoiceField(choices=TITLE_CHOICES)
def clean_username(self): # check if username dos not exist before
try:
User.objects.get(username=self.cleaned_data['username']) #get user from user model
except User.DoesNotExist :
return self.cleaned_data['username']
raise forms.ValidationError("this user exist already")
def clean(self): # check if password 1 and password2 match each other
if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:#check if both pass first validation
if self.cleaned_data['password1'] != self.cleaned_data['password2']: # check if they match each other
raise forms.ValidationError("passwords dont match each other")
return self.cleaned_data
def save(self): # create new user
new_user=User.objects.create_user(username=self.cleaned_data['username'],
first_name=self.cleaned_data['first_name'],
last_name=self.cleaned_data['last_name'],
password=self.cleaned_data['password1'],
email=self.cleaned_data['email'],
)
return new_user
क्या यह ठीक है: मैं इस समस्या के लिए निम्नलिखित Django रूप कोडित? हालांकि यह मुझे first_name और last_name में एक त्रुटि देता है। कहते हैं कि django पहले_नाम और last_name को सहेजने() विधि में अपेक्षा नहीं करता है।
मुझे यकीन नहीं है कि आपके पास सामान्य रूप से 'सेव' विधि तक पहुंच है। यह निश्चित रूप से https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method – super9