-Django-得了"不能分配"你' Wing'"" Truck.MODEL"必须是" MODEL"实例"在为数据库

时间:2017-08-07 08:42:25

标签: python html django

models.py:

class MODEL(models.Model):
    ModelName = models.CharField(max_length = 128, unique = True)
    URL_Slug = models.SlugField(unique = True)
    ...

class Maker(models.Model):
    maker = models.CharField(max_length = 128, unique = True)
    URL_Slug = models.SlugField(unique = True)
    ...
class Truck(models.Model):
    MODEL = models.ForeignKey(MODEL)
    CarID = models.CharField(max_length = 10, unique = True, null = False)
    Maker = models.ForeignKey(Maker)   
    year = models.IntegerField(default = 0)
    def __unicode__(self):
        return self.CarID

forms.py:

class MODELForm(forms.ModelForm):
    ModelName = forms.CharField(max_length = 128, help_text = 'Enter the NEW model name')
    URL_Slug = forms.SlugField(widget = forms.HiddenInput(), required = False)

    class Meta:
        model = MODEL
        fields = ('ModelName', )

class MakerForm(forms.ModelForm):
    maker = forms.CharField(max_length = 128, help_text = 'Enter the NEW maker')
    URL_Slug = forms.SlugField(widget = forms.HiddenInput(), required = False)

    class Meta:
        model = Maker
        fields = ('maker', )

class TruckForm(forms.ModelForm):
    MODEL = forms.CharField(max_length = 128, help_text = 'Enter the NEW model name')
    CarID = forms.CharField(max_length = 10, help_text = 'Enter the NEW truck ID')
    Maker = forms.CharField(max_length = 128, help_text = 'Enter the NEW maker')   
    year = forms.IntegerField(help_text = 'Enter the year of production')

    class Meta:
        model = Truck
        fields = ('MODEL', 'CarID', 'Maker', 'year')

views.py:

def add_Truck(request):
    form = TruckForm()

    if request.method == 'POST':
        form = TruckForm(request.POST)
        if form.is_valid():
            form.save(commit = True)
            return index(request)

        else:
            print(form.errors)
    return render(request, 'APP/addTruck.html', {'form': form})

HTML文件:

<!DOCTYPE html>
<html>
    <body>
        <div>
            <form id = "Truck" method = "post" action = "/APP/NewTruck/">
                {%csrf_token%}
                {% for field in form.visible_fields %}
                    {{field.errors}}
                    {{field.help_text}}
                    {{field}}<br/>
                {% endfor %}
                <input type = "submit" name = "submit" value = "Create Truck">
            </form>
        </div>
    </body>
</html>

我正在尝试在addTruck.html中创建一个新对象,因为MODEL和Maker是外键,因此在创建Truck对象时,MODEL和Maker字段也应该是对象。

回溯:

这是来自终端Macbook的Traceback:

-

  

[07 / Aug / 2017 09:14:04]&#34; GET / APP / NewTruck / HTTP / 1.1&#34; 200 1240
  内部服务器错误:/ APP / NewTruck / Traceback(最近的呼叫
  最后):文件
  &#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/exception.py" ;,   第39行,在内部          response = get_response(request)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py",   第187行,在_get_response中          response = self.process_exception_by_middleware(e,request)文件
  &#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" ;,   第185行,在_get_response中          response = wrapped_callback(request,* callback_args,** callback_kwargs)File&#34; /Users/apple/WebWorkshop/TKTrading/APP/views.py" ;,第88行,中
  add_Truck          if form.is_valid():File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py",   第169行,in_valid          返回self.is_bound而不是self.errors文件&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py",   第161行,有错误          self.full_clean()File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/forms.py",   第372行,在full_clean中          self._post_clean()File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/models.py",   第388行,在_post_clean中          self.instance = construct_instance(self,self.instance,opts.fields,opts.exclude)文件
  &#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/forms/models.py" ;,   第60行,在construct_instance中          f.save_form_data(instance,cleaning_data [f.name])File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/ 初始化的.py&#34 ;,   第833行,在save_form_data中          setattr(instance,self.name,data)File&#34; /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py&# 34 ;,   第211行,设置          self.field.remote_field.model._meta.object_name,ValueError:无法分配&#34;你&#39; Wing&#39;&#34;&#34; Truck.MODEL&#34;必须是&#34; MODEL&#34;实例。
  [07 / Aug / 2017 09:14:42]&#34; POST / APP / NewTruck / HTTP / 1.1&#34; 500 93290

1 个答案:

答案 0 :(得分:1)

TruckForm

MODEL = forms.CharField(max_length = 128, help_text = 'Enter the NEW model name')

在你的模特中,

MODEL = models.ForeignKey(MODEL)

ForeignKey字段必须是相应模型的实例,而不是纯文本。错误消息非常具体。在您的情况下,如果您想每次为新卡车创建新模型,您可能需要覆盖save()方法,因为适当的原因。或者如果你想从现有的ModelForm中选择一个模型,你可以让Django使用该字段的默认选择小部件,或者你也可以通过定义一个自定义小部件来覆盖它。

MODELs