我正在编写四个表格:
class Product(models.Model):
active = models.BooleanField(default=True)
name = models.CharField(max_length=40, unique=True)
acronym = models.CharField(max_length=3, unique=True)
class Alarm(models.Model):
active = models.BooleanField(default=True)
product = models.ForeignKey(Product) #NEW
name = models.CharField(unique=True, max_length=35)
image_file = models.CharField(max_length=30)
class File(models.Model):
reference = models.CharField(max_length=40)
product = models.ForeignKey(Product, related_name= item_product ) #NEW
created = models.DateTimeField(default=datetime.now)
created_by = models.ForeignKey(User)
class FileAlarm(models.Model):
file = models.ForeignKey(File)
created = models.DateTimeField()
created_by = models.ForeignKey(User, related_name="alarm_created_by")
alarm_date = models.DateField(default=datetime.now)
alarm_type = models.ForeignKey(Alarm)
alarm_comment = models.CharField(max_length=80, blank=True, null=True)
无需改动。 文件Alarm.alarm_type (这意味着queryset
in _init
nkt work)因此,我需要填满choices=
ps of Choice Field
。 我很想知道,如何做到这一点,因为我只想发出以下警报:active
,并与<条码><<>>>>产品/代码”挂钩,该编码与<条码>
class FileAlarmForm(ModelForm):
def __init__(self,file,*args,**kwargs):
super(FileAlarmForm, self).__init__(*args, **kwargs)
self.fields[ alarm ].queryset = Alarm.objects.filter(product__id=file.product_id)
alarm_type = ChoiceField(required=True, widget=Select(attrs={ onChange : updateAlarmImage })) # << ?!?