开发者

Django ModelForm fields display based on whether related FK fields are used

I am creating an application where users can set objects and the corresponding data they want to record. Let me give you an example:

class vehicle_type(models.Model):
    name = models.CharField(max_length=20, unique=True)
    int1_name = models.CharField(max_length=20, blank=True, null=True)
    int1_default = models.IntegerField(blank=True, null=True)
    int2_name = models.CharField(max_length=20, blank=True, null=True)
    int2_default = models.IntegerField(blank=True, null=True)
    float1_name = models.CharField(max_length=20, blank=True, null=True)
    float1_default = models.FloatField(blank=True, null=True)

class vehicle(models.Model):
  开发者_如何转开发  registration = models.CharField(max_length=20)
    vehicle_type = models.ForeignKey(vehicle_type)
    int1_val = models.IntegerField(blank=True, null=True)
    int2_val = models.IntegerField(blank=True, null=True)
    float1_val = models.FloatField(blank=True, null=True)

Where the data would be something like this to describe the vehicles:

    # pseudo code
    vehicle_type('Car','Seats',4,'Doors',4,'',)
    vehicle_type('Van','Seats',2,'',,'Load',3.2)

and then the data on the vehicles would be:

    vehicle('ABC 123',1,2,2,) #sports car
    vehicle('DEF 456',1,6,,) #SUV
    vehicle('GHI 789',2,,,1.2) #light van
    vehicle('JKL 246',2,4,3,3.6) #heavy van

My question is how can I make sure that the vehicleForm does not display the fields NOT required by the vehicle_type? I know I could pass an instance of the vehicle_type to the vehicleForm template and only display elements of the vehicleForm if they are declared in the vehicle_type, but this seems unnecessary and moves logic into the template. (I hope all this makes sense)


You can pass vehicle_type to VehicleForm.__init__(), and put there some custom logic which would, for example, set widgets of fields that are not required to HiddenInput.

Basic example (code is not tested)::

class VehicleForm(forms.ModelForm):
    def __init__(self, vehicle_type, *args, **kwargs):
        if vehicle_type.name == 'Car':
            self.fields[some_field_for_trucks].widget = forms.HiddenInput()
        super(VehicleForm, self).__init__(*args, **kwargs)

And when you instantiate VehicleForm in you view, you just pass as a first argument your the vehicle_type selected by user.

See examples of dynamic forms:

  • dynamically add field to a form
  • Django dynamic Form example
  • http://snippets.dzone.com/posts/show/7936 (includes setting widget attributes)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜