Django ModelForm在排除字段上返回数据库错误

时间:2014-03-02 06:58:45

标签: django django-forms

这是models.py:

class User(TimeStampedModel):
    id = models.AutoField(primary_key=True)

class Shoe(models.Model):
    size = models.IntegerField(help_text="The shoe\'s size", choices=sizeChoices)
    user = models.ForeignKey('User', related_name='shoes')
    brand = models.ForeignKey('Brand', related_name='shoes')

class Brand(models.Model):
    name = models.CharField(max_length=64, choices = brandChoices)

class ShoeForm(ModelForm):
    def __init__(self, *args, **kwargs):
        super(ShoeForm, self).__init__(*args, **kwargs)
        self.fields['brand'].required = True
        self.fields['size'].required = True
    class Meta:
        model = Shoe
        fields = ['brand', 'size']
        exclude = ['user']

views.py:

def index(request):
    ShoeFormSet = modelformset_factory(Shoe, form=ShoeForm, extra=3)
    NewShoeFormSet = modelformset_factory(Shoe, form=NewShoeForm)
    if request.method == 'POST':
        shoe_formset = ShoeFormSet(request.POST, request.FILES, prefix='shoes')
        new_shoe_formset = NewShoeFormSet(request.POST, request.FILES, prefix='new_shoes')
        if shoe_formset.is_valid() and new_shoe_formset.is_valid():
            #do something with the cleaned data
    else:
        shoe_formset = ShoeFormSet(prefix='shoes')
        new_shoe_formset = NewShoeFormSet(prefix='new_shoes')
    return render(request, 'shoes/input_shoes.html', {
        'shoe_formset': shoe_formset,
        'new_shoe_formset': new_shoe_formset,
        })

但是我收到此错误:DatabaseError: no such column: shoes_shoe.user_id,在表单呈现时出现在行shoe_formset = ShoeFormSet(prefix='shoes')上。出了什么问题?

0 个答案:

没有答案