#30260: ManyToManyField with a non-list default value has different behavior in
an
inlined Admin vs a standalone Admin
-------------------------------------+-------------------------------------
Reporter: Landcross | Owner: nobody
Type: Bug | Status: new
Component: | Version: 2.1
Uncategorized | Keywords: ManyToManyField,
Severity: Normal | Inline, Admin
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/30260>
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 post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/052.3f20a1756390776ab2a0488d5b85bba0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.