1

我的models.py 中有一个名为INPUT 的模型。它有多对多字段我的问题是我的值没有保存在数据库或表名中 INPUT 我不知道我错在哪里。

我的models.py是:

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Person(models.Model):
        title=models.CharField(max_length=1000,unique=True)
    cost=models.CharField(max_length=100)
    desc=models.TextField(blank=True)
    color=models.CharField(max_length=1000)
    image_urls=models.CharField(max_length=1000)
    source=models.CharField(max_length=1000,unique=True)
    category=models.CharField(max_length=1000)
    size=models.CharField(max_length=1000)
    fit=models.CharField(max_length=1000)
    fabric=models.CharField(max_length=1000)
    type_dress=models.CharField(max_length=1000)
    currency=models.CharField(max_length=1000)
    advertiser=models.CharField(max_length=1000)
    def __unicode__(self):
            return self.title

class foroccasion(models.Model):
        occasions=models.CharField(max_length=2000)
    def __unicode__(self):
            return self.occasions

class forstyle(models.Model):
        style=models.CharField(max_length=2000)
    def __unicode__(self):
            return self.style

class forbodytype(models.Model):
        bodytype=models.CharField(max_length=2000)
    def __unicode__(self):
            return self.bodytype

class  Input(models.Model):
    tabbody=[
            ("apple","apple"),
            ("pear/triangle","pear/triangle"),
            ("hourglass","hourglass"),
            ("rectangle","rectangle"),
            ("inverted triangle","invered triangle")
        ]
        tabstyle=[
        ("vintage","vintage"),
        ("sophisticated","sophisticated"),
        ("chic","chic"),
        ("bone","bone"),
        ("edgy","edgy")
        ]

#   apparelid=models.(max_length=104)
    apparelid=models.ForeignKey('Person')
#   apparelid=models.CharField(max_length=100,blank=True)
    userid=models.CharField(max_length=100,blank=True,editable=False)
    occassion=models.ManyToManyField(foroccasion)
    bodytype=models.ManyToManyField(forbodytype)
    style=models.ManyToManyField(forstyle)
    def __unicode__(self):
            return self.userid

我的forms.py如下:

import re
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from apple3.models import models 
from django.forms import ModelForm
from apple3.models import Input,Person

class UserForm(ModelForm):
    class Meta:
    model=Input

我的views.py如下:

def title(request,title_id):
        cur=0
    cur=Person.objects.get(pk=title_id)
#   imge=Person.objects.all()[:109]
#   for i in range(1,len(imge)-2):
#       if i==int(title_id):
#           cur=imge[i]
    sz=str(cur.size)
    sz=sz.split(',')
    fsz=[]
    for i in sz:
       s=""
       for j in range(0,len(i)):
        if i[j]!='U' and i[j]!='K' and i[j]!=',':
            s+=i[j]
       fsz.append(s)
    lsz=len(fsz)
#form=UserForm(request.POST)
        if request.method == 'POST':# If the form has been submitted...
        print "dsflkjdfslkjdsl"
            form=UserForm(request.POST)# A form bound to the POST data
            if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
#form.cleaned_data()
            new=form.save(commit=False)
            new.userid=request.user
            new.occassion=self.cleaned_data["occassion"]
            new.bodytype=self.cleaned_data["bodytype"]
            new.style=self.cleaned_data["style"]
            new.apparelid=self.cleaned_data["apparelid"]
            new.save()
            form.save_m2m()
        else:
            form = UserForm() # An unbound form"""
        template=loader.get_template('apple3/title.html')
    context=RequestContext(request,{
            'cur':cur,
            'sz':fsz,
            'lsz':lsz,
            'form':form,
        }
        )
        return HttpResponse(template.render(context))

我的模板如下:

<form action="/dress/female/" method="post">{% csrf_token %}

{{form.as_p}}
<input type="submit" value="Submit" />
</form>

我也尝试过显示表单的各个元素,但它没有保存在数据库中请帮助我,我是 django 的新手。

4

1 回答 1

0

在您的表单上添加类似这样的内容:

  YOURFORM(ModelForm):

    class Meta:
    model=Yourmodel

    def __init__(self, *args, **kwargs):
            self.user = kwargs.pop('user', None)
            super(yourform, self).__init__(*args, **kwargs)

    def save(self, commit=True):
        instance = super(yourform, self).save(commit=False)
        if self.user:
            instance.user = self.user
        return instance.save()

在我的示例中,我将 USER 字段设置为从以下视图调用的值:

        form = myForm(request.POST,user=request.user)
于 2013-10-26T13:36:48.903 回答