这就是我的models.py,我想知道这是不是为外键id设置关联的正确方式?这个想法是让每个hardareid_text包含多个条形码is。
import datetime
from django.db import models
from django.utils import timezone
class Hardwareid(models.Model):
hardwareid_text = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=timezone.now)
def __unicode__(self):
return self.hardwareid_text
class Barcodeid1(models.Model):
barcode1 = models.ForeignKey(Hardwareid)
Barcodeid1_text = models.CharField(max_length=50)
#pub_date = models.DateField(default=timezone.now)
def __unicode__(self):
return self.Barcodeid1_text
class Barcodeid2(models.Model):
barcode2 = models.ForeignKey(Hardwareid)
Barcodeid2_text = models.CharField(max_length=50)
def __unicode__(self):
return self.Barcodeid2_text我设置了一个视图,并通过一个模板将它连接起来,这样用户就可以添加Hardwareid_text的文本描述,我可以看到它被输入到mysql中,输入了id、硬件id文本和日期戳。
class HardwareidForm(forms.ModelForm):
class Meta:
model = Hardwareid
fields = ['hardwareid_text']
def get_barcode(request):
form = HardwareidForm()
if request.method == 'POST':
form = HardwareidForm(request.POST)
if form.is_valid():
the_new_entry = form.save()
return redirect('/successpage/')
else:
form = HardwareidForm()
return render(request, 'get_barcode.html', {'form': form}) 但我不确定如何添加barcodeid_texts并将它们与单个hardwareid_text相关联。最好的方法是查找Hardwareid条目,然后在该点添加barcodeid吗?如果是,我如何确保它们被附加/关联到正确的hardwareid_text数据库条目?
感谢您对此的任何帮助或建议。
编辑:我更改了我的forms.py,但它仍然给我一个错误:
class HardwareForm(forms.ModelForm):
class Meta:
model = Hardware
fields = ['text']
class BarcodeForm(forms.ModelForm):
class Meta:
model = Barcode
fields = ['text', 'hardware']来自页面访问的错误是:
ValidationError at /get_barcode.html
[u'ManagementForm data is missing or has been tampered with']
Request Method: POST
Request URL: http://127.0.0.1:8000/get_barcode.html
Django Version: 1.7
Exception Type: ValidationError
Exception Value:
[u'ManagementForm data is missing or has been tampered with']
Exception Location: C:\Python27\lib\site-packages\django\forms\formsets.py in management_form, line 96
Python Executable: C:\Python27\python.exe
Python Version: 2.7.8
Python Path:
['C:\\hwdb\\mysite',
'C:\\Python27\\lib\\site-packages\\setuptools-5.7-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.5-py2.7-win32.egg',
'C:\\windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages']
Server time: Mon, 24 Nov 2014 12:46:01 -0800编辑:添加get_barcode.html页面
<h1>Please enter the name of the Parent Hardware System<h1>
<form action='/get_barcode.html' method='post'>{% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Submit' />
</form>发布于 2014-11-24 18:00:49
不是的。您不需要多个条形码模型。您拥有从条形码到硬件的ForeignKey这一事实已经允许您为每个硬件拥有多个条形码: ForeignKey定义了多对一关系。此外,根据外键所在的模型来命名外键也没有任何意义:它需要根据它所指向的模型来命名,即硬件。最后,不要在字段名前加上模型名:这是不必要的。
class Hardware(models.Model):
text = models.CharField(max_length=200)
pub_date = models.DateTimeField(default=timezone.now)
def __unicode__(self):
return self.text
class Barcode(models.Model):
hardware = models.ForeignKey(Hardware)
text = models.CharField(max_length=50)
#pub_date = models.DateField(default=timezone.now)
def __unicode__(self):
return self.text现在,为了与硬件一起输入条形码,您需要使用inline formset。
from django.forms.models import inlineformset_factory
def get_barcode(request):
BarcodeFormSet = inlineformset_factory(Hardware, Barcode, form=BarcodeForm)
hardware = Hardware()
if request.method == 'POST':
form = HardwareForm(request.POST, instance=hardware)
formset = BarcodeFormSet(request.POST, instance=hardeware)
if form.is_valid() and formset.is_valid():
hardware = form.save()
barcodes = formset.save()
return redirect('/successpage/')
else:
form = HardwareForm(instance=hardware)
formset = BarcodeFormSet(instance=hardeware)
return render(request, 'get_barcode.html', {'form': form, 'formset': formset}) https://stackoverflow.com/questions/27101928
复制相似问题