某些模型字段在django表单中被排除。当我使用form.save()
时,这些字段的现有值会自动替换为空字符串。
但根据django doc:
“上述逻辑未包含在表单中的任何字段都不会通过表单的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)
问候。
答案 0 :(得分:0)
排除的字段activation_key
和slug
为CharField
。它们将存储字符串值。
设置null=True
时,它允许NULL
值保存在数据库中。使用blank=True
时,此字段将设置为可选字段。
允许NULL
CharField
即VARCHAR
是不好的做法。您在同一列中有NULL
个空字符串值''
。它可能导致许多不必要的副作用。
如果不需要,CharField
仅限blank=True
first_name
。 Django默认将它们保存为空字符串。所以没有任何问题。
你的问题是模型形式的错误概念。您不应该重新定义表单中的模型字段。
例如,如果需要null=True, blank=True
,那么您在模型字段中不应该有[[a , b ], [c, d]]
。您还可以在模型中设置小部件和选项。