English 中文(简体)
Django居民区
原标题:Django populating a choice field

我正在编写四个表格:

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 }))  # << ?!?
最佳回答

接下来是:

class FileAlarmForm(ModelForm):

    def __init__(self,file,*args,**kwargs):
        super(FileAlarmForm, self).__init__(*args, **kwargs)
        p = Product.objects.get(id=file.product_id)
        self.fields[ alarm_type ].widget = Select(attrs={ onchange : updateAlarmImage })
        self.fields[ alarm_type ].queryset = Alarm.objects.filter(product=p)
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签