我有一个ArrayField
的模型,我希望使用ModelForm
保存数据。但我的问题是每次只有数组的第一个值是保存。如何从表单中保存整个数组?这是我的代码:
模型:
class Reminder(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
active = models.BooleanField(default=True)
provider_type = (
('Em', 'Email'),
('Sm', 'SMS'),
('De', 'Desktop'),
('Mo', 'Mobile'),
)
notification = ArrayField(models.CharField(choices=provider_type, max_length=2, default='Em'))
形式:
class ReminderForm(ModelForm):
class Meta:
model = Reminder
exclude = ('user', )
视图:
def create(request):
if request.method == 'POST':
reminder_form = ReminderForm(request.POST)
if reminder_form.is_valid():
form = reminder_form.save(commit=False)
user = get_object_or_404(User, username=request.user.username)
form.user = user
form.save()
else:
.....
return redirect('create')
return render(request, 'add.html')