Django的ForeignKey实例错误异常

时间:2018-10-17 20:16:03

标签: django

注册人事表格时出现此错误; “异常值:无法分配“ 5”:“ db_PERSONEL.db_departman”必须是“ db_DEPARTMAN”实例。 谢谢。

departman_app.model.py

from django.db import models

class db_DEPARTMAN(models.Model):
    id = models.AutoField(primary_key=True, editable=False)
    db_departmanAdi = models.CharField(max_length = 50)

    def __unicode__(self):
        return u'%s' % (self.db_departmanAdi)

personel_app.models.py

from django.db import models
from departman.models import db_DEPARTMAN

class db_PERSONEL(models.Model):
    id = models.AutoField(primary_key=True, editable=False)
    db_personelAdi  = models.CharField(max_length = 50)
    db_departmanAdi = models.ForeignKey(db_DEPARTMAN, on_delete=models.DO_NOTHING, related_name="tags")

    def __unicode__(self):
        return u'%s' % (self.db_departmanAdi)

personel_app.personelform.py

from django import forms
from personel.models import db_PERSONEL

class personelForm(forms.ModelForm):
    class Meta:
        model=db_PERSONEL
        #fields="__all__"
        fields = [
            'db_personelAdi',
            'db_departmanAdi',
        ]

form = personelForm()
form.base_fields['db_personelAdi'] = forms.CharField(label='Personel Adı:', max_length=50,                                              widget=forms.TextInput(attrs={'class': 'form-control'}))

form.base_fields['db_departmanAdi'] = forms.IntegerField(label='Deaprtman Adı:', widget=forms.TextInput(attrs={'class': 'form-control'}))

0 个答案:

没有答案