2017-09-19 15 views
0

モデルフィールドのいくつかは、django形式で除外されています。 form.save()を使用すると、これらのフィールドの既存の値は自動的に空の文字列に置き換えられます。djangoは、除外されたモデルフィールドに空の値を設定します。

しかし、ドキュメントをジャンゴによると

「上記のロジックで、フォームに含まれていないフィールドは、フォームのsave()メソッドによって設定されることはありません」

ない私が間違っているのかわから。

class ProfileEditForm(forms.ModelForm): 

    first_name = forms.CharField(
     required=True, 
     widget=forms.TextInput(attrs={ 
      'class': 'form-control', 'placeholder': 'First Name' 
     }) 
    ) 
    last_name = forms.CharField(
     required=True, 
     widget=forms.TextInput(attrs={ 
      'class': 'form-control', 'placeholder': 'Last name' 
     }) 
    ) 
    division = forms.ChoiceField(
     required=True, 
     choices=[(x, x) for x in range(1, 32)], 
     widget=forms.Select(attrs={'class': 'form-control'}) 
    ) 
    city = forms.ChoiceField(
     required=True, 
     choices=[(x, x) for x in range(1, 32)], 
     widget=forms.Select(attrs={'class': 'form-control'}) 
    ) 
    thana = forms.ChoiceField(
     required=True, 
     choices=[(x, x) for x in range(1, 32)], 
     widget=forms.Select(attrs={'class': 'form-control'}) 
    ) 
    national_id = forms.CharField(
     required=False, 
     widget=forms.TextInput(attrs={ 
      'class': 'form-control', 'placeholder': 'National ID' 
     }) 
    ) 
    passport_number = forms.CharField(
     required=True, 
     widget=forms.TextInput(attrs={ 
      'class': 'form-control', 'placeholder': 'Passport No' 
     }) 
    ) 

    class Meta: 
     model = Profile 
     exclude = ['user', 'activated', 'activation_key', 'slug', 'created'] 
     # fields = ('username', 'email',) 
     # fields = ('first_name', 'last_name', 'division', 'city', 'thana', 'national_id', 'passport_number') 

    def save(self, commit=True): 
     # Save the provided password in hashed format 
     print("in save!") 
     profile = super(ProfileEditForm, self).save(commit=False) 
     profile.activated = True 
     print(str(profile.activation_key)) 
     if commit: 
      profile.save() 
     return profile 

モデル

User = get_user_model() 

class Profile(models.Model): 
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) # user.profile 
    first_name = models.CharField(max_length=120, blank=True, null=True) 
    last_name = models.CharField(max_length=120, blank=True, null=True) 
    division = models.CharField(max_length=250, null=True, blank=True) 
    city = models.CharField(max_length=250, null=True, blank=True) 
    thana = models.CharField(max_length=250, null=True, blank=True) 
    national_id = models.CharField(max_length=250, null=True, blank=True) 
    passport_number = models.CharField(max_length=250, null=True, blank=True) 
    timestamp = models.DateTimeField(auto_now_add=True, null= True) 
    updated = models.DateTimeField(auto_now=True) 
    created = models.BooleanField(default=False) 
    activated = models.BooleanField(default=False) 
    activation_key = models.CharField(max_length=200, null= True, blank= True) 
    slug = models.SlugField(null=True, blank=True) 

    def __str__(self): 
     return self.user.username 


def post_save_user_receiver(sender, instance, created, *args, **kwargs): 
    print("In rl_post _save" + str(instance.slug) + str(instance.activation_key) + str(instance.activated)) 
    if created: 
     profile, is_created = Profile.objects.get_or_create(user=instance) 

post_save.connect(post_save_user_receiver, sender=Profile, dispatch_uid="rl_pre_save_receiver") 

def rl_pre_save_receiver(sender, instance, *args, **kwargs): 
    # instance.category = instance.category.capitalize() 
    print("In rl_pre _save"+ str(instance.slug) + str(instance.activation_key) + str(instance.activated)) 

    if not instance.slug: 
     instance.slug = unique_slug_generator(instance) 

pre_save.connect(rl_pre_save_receiver, sender=Profile) 

よろしく:ここに私のフォームとモデルがあります。

+1

どのフィールドが上書きされていますか? –

+0

@AamirAdnan activation_key、slug、activated –

+0

@MasudulHasanこれらのフィールドにはどの値が保存されますか? – cezar

答えて

0

除外されたフィールドactivation_keyslugは、CharFieldです。彼らは文字列の値を格納します。

null=Trueを設定すると、NULLの値をデータベースに保存できます。 blank=Trueでは、このフィールドはオプションに設定されています。

NULLCharFieldにすると、つまりVARCHARとなることが悪い習慣です。 NULLと同じ列に空の文字列値''があります。多くの望ましくない副作用を引き起こす可能性があります。

CharFieldは、必要でない場合はblank=Trueとしてください。 Djangoはデフォルトで空文字列として保存します。だからそれには何も悪いことはない。

あなたの問題は、むしろモデルフォームの誤解です。フォーム内のモデルフィールドを再定義するべきではありません。

たとえば、first_nameが必要な場合は、モデルフィールドにnull=True, blank=Trueを設定しないでください。モデルのウィジェットと選択肢を設定することもできます。