[formset] ManyToMany

5 views
Skip to first unread message

Robin Riis

unread,
Apr 9, 2019, 3:27:31 AM4/9/19
to django...@googlegroups.com
if i have the following models:

class Wiggs_Filter(models.Model):
    wiggs_nr = models.CharField(max_length=30, unique=True, blank=True, null=True)
    picture = models.ImageField(upload_to='filter/', default='filter/saknas.jpg')
    description = models.CharField(max_length=100, blank=True, null=True)
    outer_diameter = models.CharField(max_length=25, blank=True, null=True)
    inner_diameter = models.CharField(max_length=25, blank=True, null=True)
    use_with = models.ManyToManyField("self", blank=True)

class Cross_Filter(models.Model):
    wiggs = models.ManyToManyField(Wiggs_Filter, related_name='cross_ref')
    manufacture = models.CharField(max_length=25, choices=TILLVERKARE)
    name = models.CharField(max_length=25)


and i would like to be able to open a modal window in Wiggs_Filter DetailView
with a formset to Cross_Filter but i cannot get it to work
have the following form:

class FilterCrossForm(forms.ModelForm):
    class Meta:
        model = Filter_Cross
        fields = '__all__'

CrossFormset = forms.formset_factory(FilterCrossForm)

class AddFilterAjaxForm(forms.ModelForm):
    class Meta:
        model = Wiggs_Filter
        exclude = ('picture', 'use_with')

and this is my DetailView:

class Wiggs_Filtret(DetailView):
    template_name = 'lager/detta_filter.html'
    model = Wiggs_Filter

    def get_object(self):
        return get_object_or_404(Wiggs_Filter, wiggs_nr=self.kwargs['wiggs_nr'])

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['cross_options'] = self.get_cross_manufacturer()
        context['cross_refs'] = self.object.cross_ref.all()
        return context

    def get_cross_manufacturer(self):
        cross = [obj.manufacture for obj in self.object.cross_ref.all()]
        return list(dict.fromkeys(cross))

any tips on how to open a bootstrap modal with the CrossFormset formset?

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