使用modelform保存postgres arrayfield

时间:2017-05-11 04:41:05

标签: django postgresql django-models django-forms

我有一个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')

0 个答案:

没有答案