#31767: Strange behaviour with QuerySet unions with ModelMultipleChoiceField
---------------------------------------+------------------------
               Reporter:  Tom Carrick  |          Owner:  nobody
                   Type:  Bug          |         Status:  new
              Component:  Forms        |        Version:  master
               Severity:  Normal       |       Keywords:
           Triage Stage:  Unreviewed   |      Has patch:  0
    Needs documentation:  0            |    Needs tests:  0
Patch needs improvement:  0            |  Easy pickings:  0
                  UI/UX:  0            |
---------------------------------------+------------------------
 I came across this issue on Stack Overflow. I'm not 100% sure it's a bug,
 but it does seem strange. With this code (excuse the bizarre example
 filtering):

 {{{
 class Publication(models.Model):
     pass

 class Article(models.Model):
     publications = models.ManyToManyField(to=Publication, blank=True,
 null=True)

 class ArticleForm(forms.ModelForm):
     publications = forms.ModelMultipleChoiceField(
         Publication.objects.filter(id__lt=2) |
 Publication.objects.filter(id__gt=5),
         required=False,
     )

     class Meta:
         model = Article
         fields = ["publications"]

 class ArticleAdmin(admin.ModelAdmin):
     form = ArticleForm
 }}}

 This works well. However, changing the `ModelMultipleChoiceField` queryset
 to use `union()` breaks things.

 {{{
 publications = forms.ModelMultipleChoiceField(
     Publication.objects.filter(id__lt=2).union(
         Publication.objects.filter(id__gt=5)
     ),
     required=False,
 )
 }}}

 The form correctly shows only the matching objects. However, if you submit
 this form while empty (i.e. you didn't select any publications), ALL
 objects matching the queryset will be added. Using the `OR` query, NO
 objects are added, as I'd expect.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31767>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.eeaf7f30f748e669713badd11cecbae6%40djangoproject.com.

Reply via email to