在 Django 1.7 上使用表单和视图更新模型字段

Updating fields of model using forms and views on Django 1.7

在models.py我的产品型号是

class Product(models.Model):
    productName = models.CharField(max_length=20, default='1',blank=False,null=False, primary_key = True)
    userPhone = models.CharField(max_length=20, default='1')
    userid = models.ForeignKey(Account, default='1',null=True)
    productDesc = models.TextField(blank=False,null=False, default='1')
    productCategory = models.ForeignKey(Category, null=False, default='1')
    productPrice = models.DecimalField(default='0',blank=False,null=False, max_digits=6, decimal_places=2)
    picture = models.ImageField(upload_to='product_images', blank=True, null=True, default='1')

    def __unicode__(self):
        return self.productName

我必须添加新产品的表格是,

class ProductForm(forms.ModelForm):
    productName = forms.CharField(label='Product Name')
    productCategory = forms.ModelChoiceField(label='Category', queryset=Category.objects.all())
    productDesc = forms.CharField(label='Product Description', widget=forms.Textarea)
    productPrice = forms.DecimalField(label='Expected Price')
    userPhone = forms.CharField(label='Phone Number')
    picture = forms.ImageField(label='Upload Picture')

    class Meta:
        model = Product
        fields = ('productName', 'productCategory', 'productDesc', 'productPrice', 'userPhone', 'picture',) 

    def clean_productName(self):
        productName = self.cleaned_data['productName']
        try:
            Product.objects.get(productName=productName)
        except Product.DoesNotExist:
            return productName
        raise forms.ValidationError("A product under that name already exits. Rename your product.")

    def clean_productCategory(self):
        productCategory = self.cleaned_data['productCategory']

    def clean_productDesc(self):
        productDesc = self.cleaned_data['productDesc']

    def clean_productPrice(self):
        productPrice = self.cleaned_data['productPrice']

    def clean_userPhone(self):
        userPhone = self.cleaned_data['userPhone']

    def clean_picture(self):
        picture = self.cleaned_data['picture']

为了获取表单输入,我在 views.py 文件中有

@login_required(login_url='/accounts/login/')   
def newProduct(request):
    if(request.method =='POST'):
        product_form = ProductForm(request.POST, request.FILES)
        if product_form.is_valid():
            product = product_form.save(commit=True)
            product.save()
        else:
            print product_form.errors

    else:
        product_form = ProductForm()
    return render(request, 'market/postad.html', {'product_form':product_form} )

我想将产品模型的 userid 字段更新为登录用户的 user_id。我该怎么做呢?

request.user.id

可能会给我登录用户的 ID。但是我如何将其与输入数据库的产品关联起来呢? (我正在使用 MySQL 数据库)

我的 AUTH_USER_MODEL 未配置为帐户。有没有不配置的方法?

我的账户模型是

class Account(models.Model):
    user = models.OneToOneField(User)

我从 django.contrib.auth.models

导入了用户

在 views.py 中(如果您的 AUTH_USER_MODEL 配置为帐户)

@login_required(login_url='/accounts/login/', template_name='market/postad.html')   
def newProduct(request):
    product_form = ProductForm(request.POST or None, request.FILES or None)
    if(request.method =='POST'):
        if product_form.is_valid():
            product = product_form.save(commit=False)
            product.userid = Account.objects.get_or_create(user=request.user)
            product.save()
        else:
            print product_form.errors
    return render(request, template_name, {'product_form':product_form} )