带有Through参数的Django ForeignKey [英] Django ForeignKey with through parameter
问题描述
最初我的问题是如何使字段显示ModelForm中的Foreignkey. 我已经用这段代码解决了这个问题,但是我无法使它正常工作.
My problem originally was how to make fields show of a Foreignkey that is in ModelForm. I have solved this problem with this code but I can't get it to work.
以下模型构成了我的代码
The following models make my code
class DocAide(models.Model):
id = models.AutoField(primary_key=True)
pulse = models.DecimalField('Pulse', max_digits=3, decimal_places=0, blank=True, null=True)
weight = models.DecimalField('Weight (kg)', max_digits=3, decimal_places=0, blank=True, null=True)
bp_sys = models.DecimalField('BP Sys', max_digits=3, decimal_places=0, blank=True, null=True)
bp_dia = models.DecimalField('BP Dia', max_digits=3, decimal_places=0, blank=True, null=True)
temperature = models.DecimalField('Temp. deg C', max_digits=2, decimal_places=1, blank=True, null=True)
scans = models.ManyToManyField(Scan, blank=True)
tests = models.ManyToManyField(LabTest, blank=True)
date = models.DateField(editable=False, default=timezone.now)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
created_by = models.ForeignKey(Doctor, on_delete=models.CASCADE, blank=True, null=True)
no_charge = models.BooleanField('No Charge from Patient', default=False)
doctors_notes = models.TextField('Patient is complaining about:', default='')
part_to_scan = models.CharField(max_length=100, default='N/A', blank=True, null=True)
part_to_xray = models.CharField(max_length=100, default='N/A', blank=True, null=True)
note = models.TextField(max_length=100, default='')
和
class PrescriptionLine(models.Model):
drug = models.ForeignKey(to=Drug, related_name='prescriptionlines', on_delete=models.CASCADE)
doc_aide = models.ForeignKey(to=DocAide, related_name='prescriptionlines', on_delete=models.CASCADE)
morning = models.CharField(validators=[int_list_validator], max_length=3, default=0)
midday = models.CharField(validators=[int_list_validator], max_length=3, default=0)
evening = models.CharField(validators=[int_list_validator], max_length=3, default=0)
night = models.CharField(validators=[int_list_validator], max_length=3, default=0)
days = models.CharField(validators=[int_list_validator], max_length=3, default=0)
tablets = models.CharField(validators=[int_list_validator], max_length=3, default=0)
我的表单如下:
class DocAideForm(forms.ModelForm):
class Media:
css = {'all': ('/static/admin/css/widgets.css',), }
js = ('/admin/jsi18n/',)
class Meta:
model = DocAide
fields = [ # 'no_charge',
'doctors_notes', 'scans', 'part_to_xray', 'part_to_scan', 'tests', 'prescriptions']
没有处方就可以使用,但是我似乎无法从处方中访问数据.
Without the prescriptions it works but I can't seem to access the data from the Prescription.
那么,我该如何按照所需的处方字段创建表单.我想通过到"它连接到doc_aide_form.当我写这篇文章的时候,我只是有了另一个主意.我可以在doc_aide_form模板中创建一个用于处方的按钮,然后在其中创建一个完整的表单. 但是仍然如何更有效地使用此结构.
So how do I create the form the way I want of the prescriptions field. I thought through "to" it is connected to the doc_aide_form. Whilst I am writing this I just had another idea. I could create a button in the doc_aide_form template for prescription and make a whole form there. But still how do I use this structure more effectively.
谢谢
推荐答案
要创建带斜线的表单,必须使用官方网站上查看如何使用它的示例. django文档.
To create form with inlilines, you must use the inlineformset_factory. You can check an example of how to use it in the official django documentation.
models.py:
models.py:
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
views.py
from django.forms import inlineformset_factory
BookFormSet = inlineformset_factory(Author, Book, fields=('title',))
author = Author.objects.get(name='Mike Royko')
formset = BookFormSet(instance=author)
这篇关于带有Through参数的Django ForeignKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!