m2m createview bootstrap modal

18 views
Skip to first unread message

Robin Riis

unread,
Apr 26, 2019, 2:53:52 AM4/26/19
to django...@googlegroups.com
Hello Django community :D

i am currently learning django and am working on a vehicle app
where i currently have this models.py

class Engine(models.Model):
    objects = EngineManager()
    manufacture = models.CharField(max_length=80)
    name = models.CharField(max_length=80)
    filter_to_use = models.ManyToManyField('Filter') #this is in another app

    class Meta:
        unique_together = (('manufacture', 'name'),)

    def natural_key(self):
        return (self.manufacture, self.name)

    def __unicode__(self):
        return self.__str__()

    def __str__(self):
        return self.manufacture+' - '+self.name

class Vehicle_Model(models.Model):
    engine = models.ForeignKey(Engine, on_delete=models.CASCADE)
    name = models.CharField(max_length=100)
    year_from = models.IntegerField(blank=True, null=True)
    year_to = models.IntegerField(blank=True, null=True)
    serial_number_from = models.CharField(max_length=100, blank=True, null=True)
    serial_number_to = models.CharField(max_length=100, blank=True, null=True)
    picture = models.ImageField(upload_to=modell_picture_path, default='machine_models/none.png')

    ........

and i am trying to create a form for Engine.filter_to_use that will open in a modal
the form currently looks like this:

class AddEngineFilterAjaxForm(PopRequestMixin, CreateUpdateAjaxMixin, forms.ModelForm):

    class Meta:
        model = Engine
        fields = ('filter_to_use',)
        widgets = {'filter_to_use': forms.SelectMultiple(),}

    def __init__(self, *args, **kwargs):
        super(AddEngineFilterAjaxForm, self).__init__(*args, **kwargs)
        self.fields['filter_to_use'].widget.attrs['class'] = 'selectpicker form-control'
        self.fields['filter_to_use'].widget.attrs['data-live-search'] = 'true'
        self.fields['filter_to_use'].widget.attrs['title'] = 'Which filter fits in the engine?'

and i am struggling with the view and right now im pretty lost of all the trial and error :P
but i want it to have pre-selected all filters that are bound to the engine
and i want to be able to save the form.
it does look right, but the behaviour is not what i wanted.
any tips on how to write a class based view for this?

thanks in advance!
Reply all
Reply to author
Forward
0 new messages