﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
30260	ManyToManyField with a non-list default value has different behavior in an inlined Admin vs a standalone Admin	Merlijn	nobody	"First off, I'm new to this bug-tracker and I'm by no means a Django expert, I just use Django casually in my free time. So, I'm sorry if this doesn't belong in the bug tracker. Anyway, on to the problem I encountered.

In short: there seems to be a different behavior for a ManyToManyField with a non-list (integer) default value when it's in an Admin Inline vs when it's in its own Admin page. In fact, in an Inline Django crashes on save whereas on a non-inline it saves just fine.

I encountered this problem in a personal project and to check this problem I made a small test-project to test it and also help explain the problem.

Consider the following model.py:

{{{
from django.db import models


class Author(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class Series(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class Book(models.Model):
    title = models.CharField(max_length=255)
    series = models.ForeignKey(to=Series, on_delete=models.CASCADE)
    author = models.ManyToManyField(to=Author, default=3)

    def __str__(self):
        return self.title
}}}

And the following admin.py:

{{{
from django.contrib import admin

from stack.models import Book, Series, Author


class BookInline(admin.StackedInline):
    model = Book
    extra = 0


class SeriesAdmin(admin.ModelAdmin):
    inlines = [
        BookInline
    ]


admin.site.register(Book)
admin.site.register(Series, SeriesAdmin)
admin.site.register(Author)
}}}

(Note: the above example assumes you have at least 3 authors already created in the database to work properly).

Now if I were to run the above application and go to the Book admin page and create a new Book, the default value of Author works fine; the 3rd author is visibly selected and on saving it gets saved no problem whatsoever. However... If I were to go the the Series admin page (which has a Book inline) and create a new Series, the Author field seems to work just fine; the 3rd Author is selected. But, on save it crashes with the following error:

> object of type 'int' has no len()

Which originates from django\forms\models.py in has_changed, line 1351.

And it doesn't matter whether I manually select another Author, the save always crashes (unless I don't any any Book/Inline to the Series object).
Not that editing and saving a Series object with already existing Book inlines works fine (unless I try to add a new Book to the Series).

The solution to the crash is simple. Instead of default=3 on the Book model, we can change it to default=[3] and everything works like a charm. 

Now, I don't know if a non-list default value for a ManyToManyField is something that should be possible or is actually something that should not be possible, but happens to work in some cases nonetheless. However, I do think the difference in behavior between the non-inline and the inline should be the same, whether that is a crash or a functioning save."	Bug	closed	contrib.admin	2.1	Normal	invalid	ManyToManyField, Inline, Admin		Unreviewed	0	0	0	0	0	0
