filter_horizontal chosen validation field not working properly

I have ThesisExamSchedule model that I registered in admin using ModelAdmin.

class ThesisExamSchedule(models.Model):
    participant=models.ForeignKey(User,on_delete=models.CASCADE,related_name="thesis_participant")
    room=models.TextField()
    date=models.DateField()
    start_time=models.TimeField()
    end_time=models.TimeField()
    thesis_title=models.TextField()
    examiner=models.ManyToManyField(User)
    def __str__(self) -> str:
        return self.participant.first_name
class ThesisExamScheduleAdmin(admin.ModelAdmin):
    list_display=['participant','date','start_time','end_time','thesis_title']
    filter_horizontal=['examiner']
    autocomplete_fields=['participant']
admin.site.register(ThesisExamSchedule,ThesisExamScheduleAdmin)

After I choose multiple users to fill examiner field then hit save. Django admin throw empty validation error and the examiner field emptied.

Scenario 1 validation error throwed:

  1. choose users from left box.
  2. chosen user show on the right box.
  3. submit
  4. validation error throwed This field is required. on examiner field, and the field got emptied.

Scenario 2 Success:

  1. choose users from left box.
  2. chosen user show on the right box.
  3. select user (highlight the user).
  4. submit.
  5. success message appeared The thesis exam schedule “Farid Budi” was added successfully. .

Is it normal?
My expectation is there will be no error when using scenario 1

Can you share the exact validation message that the admin is printing out and for what field it’s for?

Also, can you explain what about scenario 2 makes it not break? Your scenario 1 case is missing the submit step that you included in the following statement, but the rest of scenario 1 is similar to scenario 2.

After I choose multiple users to fill examiner field then hit save. Django admin throw empty validation error and the examiner field emptied.

I edited my question there.
I included pictures to help me explain the problem.

the difference between scenario 1 and scenario 2 is in **highlighted item/user ** in chosen box (right box).

what I wanted to do is add multiple users to be the examiner then save it.

Well that’s super weird. Have you used the Browser Developer Tools to inspect the requests between the two to see if the data is being sent to the server? You can do so on the network panel.

Additionally, you could make examiner = models.ManyToManyField(User, blank=True). That would avoid the “This field is required” validation. However, I’m not sure it’ll have the values set since I don’t understand this inconsistency.

In scenario 1 the examiner field not being included in the request. is it Bug??

note: I forgot to tell you that this problem is happened when I tried to add data. when I edited data scenario 1 not throwing validation error.

  1. csrfmiddlewaretoken: 8IClVVSQjGjAkhIocWxFsvi4H5yqoylRPOkKIC3Ut4yW1LcB2CEbFnOt02pX8hUp

  2. participant: 29

  3. room: sadsad

  4. date: 2022-10-27

  5. start_time: 11:33:38

  6. end_time: 11:34:09

  7. thesis_title: sdsds

Please compare the requests for the two scenarios.

Scenario 1

Scenario 2

Did you ever try setting blank=True? This seems like it may be a frontend issue. It’s probably worth trying some other browsers to eliminate that as a possibility.

It does appear that it may be something with some JavaScript involved on the front end. I might suggest trying this in an incognito window or clearing the browser cache to ensure that current code is retrieved from the server for this request.

(You could also check the browser’s network tab to verify that an actual request is being made for all the JS and CSS files being used on that page.)

I am afraid using blank=true will lead to giving user wrong information (false positive).
the data is saved but with empty examiner.

but I will try with, different browser to see if this will work.

Note: Scenario 1 is working properly when used in User page that django provided (default User admin). But I still want to try in different browser or clearing cache and see if there is css/js file not loaded properly

Are there any other customizations to this model’s ModelAdmin and/or the template it uses?

no, I just extend ModelAdmin, and register it in admin.py

class ThesisExamScheduleAdmin(admin.ModelAdmin):
    list_display=['participant','date','start_time','end_time','thesis_title']
    filter_horizontal=['examiner']
    autocomplete_fields=['participant']
admin.site.register(ThesisExamSchedule,ThesisExamScheduleAdmin)

I’m going to second Ken’s suggestions here as well: filter_horizontal chosen validation field not working properly - #9 by KenWhitesell

already tried with incognito still not working.
and all item showed in network tab loaded successfully (responded with code 200).

To verify it’s not a memory cache issue, you need to examine each request individually. If you click on the request, you get the detail panel. Select the Headers tab and look at the Status Code.

It might say 200 (from memory cache), which would indicate that it’s using the cached version and not one retrieved from the server.

You can also examine your servers console to see if you see the requests for those elements. If the browser is retrieving them from cache, then you won’t see requests for them in the server.

Just FYI, I found this page because I have encountered the same problem and was trying to figure out why.

I’m rather new to Django, working on a CS50Web course final project. Django 4.1.

I have two custom user fields that are ManyToMany, and I set filter_horizontal on the ModelAdmin. When adding a new user in admin, like mentioned above, I move the desired items from the left to the right, but when I try to save it gives validation error that these fields are required, and the right sides are blank again… but if I move the desired items to the right, and then use shift+click to highlight those items on the right, and then save, it works fine. Also, if I say move 4 items to the right, but then only highlight one of them before saving, only that one item gets saved.

It seems like there is a bug that it only accepts items you’ve highlighted on the right, just like a standard multi-select field where you highlight the items you want to select.

Mozilla Firefox Developer 108.0b3(64-bit), Windows 10 Pro