कस्टम प्रपत्र में, कोई मॉडल के फ़ील्ड की विशिष्टता को कैसे मान्य करता है (यानी, unique=True
सेट है)?मॉडलफॉर्म का उपयोग किये बिना एक अद्वितीय = सही फ़ील्ड को कैसे सत्यापित/साफ() करें?
मुझे पता है कि Django के ModelForm स्वचालित रूप से एक validate_unique()
समारोह है कि BaseModelForm के clean()
विधि के भीतर कहा जाता है करता है - तो, जब ModelForm का उपयोग कर, यह सही ढंग से नियंत्रित किया जाएगा (के रूप में यह व्यवस्थापक में है)।
हालांकि, मैं खरोंच से अपना खुद का फॉर्म बना रहा हूं और आश्चर्यचकित हूं कि मैं इसे स्वयं कैसे संभालने के बारे में जा सकता हूं? मुझे लगता है कि मेरी सबसे बड़ी बाधा जानना है जो वस्तु प्रपत्र जब डेटा साफ किया जा रहा है से जुड़ा हुआ है ...
कुछ कोड:
class UserProfile(CreatedModifiedModel):
user = models.ForeignKey(User, unique=True)
display_name = models.CharField('Display Name',max_length=30,
blank=True,unique=True)
class EditUserProfileForm(forms.Form):
display_name = forms.CharField(required=False,max_length=30)
# "notifications" are created from a different model, not the UserProfile
notifications = forms.MultipleChoiceField(
label="Email Notifications",
required=False,
widget=forms.CheckboxSelectMultiple,)
def clean_display_name(self):
# how do I run my own validate_unique() on this form?
# how do I know which UserProfile object I am working with?
# more code follows, including the __init__ which sets up the notifications
क्या कोई कारण है कि आप मॉडलफॉर्म के बजाय कस्टम फॉर्म बना रहे हैं? – tghw
मैंने "नोटिफिकेशन" फ़ील्ड दिखाने के लिए कोड अपडेट किया है, मुझे एक अलग ऐप से चाहिए, लेकिन उसी एडिट यूज़रप्रोफाइलफॉर्म पर संभाला गया है ... उम्मीद है कि यह समझ में आता है। मुझे नहीं लगता कि मैं कई मॉडल स्रोतों से मॉडलफॉर्म बना सकता हूं ... – thornomad