models.py
class Service(models.Model):
provider = models.ForeignKey("services.ServiceProvider",
verbose_name=_("Service provider"),
related_name="services")
title = models.CharField(_("Title"), max_length=150)
class Entertainment(Service):
types = models.ManyToManyField(EntertainmentType, verbose_name=_("Types"),
related_name="entertainments")
description = models.TextField(_("Description"), blank=True)
class EntertainmentPhoto(models.Model):
entertainment = models.ForeignKey(Entertainment, related_name="photos")
description = models.TextField(_("Description"), max_length=1000,
blank=True)
photo = ImageWithThumbsField(_("Photo"), sizes=(),
upload_to="services/photos/entertainment")
is_main = models.BooleanField(verbose_name=_("Main"), default=False)
class Meta:
ordering = ('-is_main',)
verbose_name = _("Photo")
verbose_name_plural = _("Photos")
**admin.py**
class EntertainmentPhotoInline(admin.TabularInline):
model = EntertainmentPhoto
class EntertainmentAdmin(CustomModelAdmin):
filter_horizontal = ["types"]
list_display = Entertainment.list_display
list_filter = ["provider", "types", ]
inlines = [EntertainmentPhotoInline]
admin.site.register(Entertainment, EntertainmentAdmin)
如果我添加一个没有任何照片的娱乐对象,并且我在编辑时添加了照片,那么一切正常。但是如果我在添加娱乐时尝试添加任何照片,我会遇到以下异常:
> Environment:
>
>
> Request Method: POST Request URL:
> http://mydomain.com/admin/entertainment/entertainment/add/
>
> Django Version: 1.5b2 Python Version: 2.7.3 Installed Applications:
> ['south', 'gunicorn', 'django_extensions',
> 'django.contrib.databrowse', 'django.contrib.contenttypes',
> 'django.contrib.sessions', 'django.contrib.messages',
> 'django.contrib.staticfiles', 'django.contrib.admin',
> 'authentication', 'framework', 'framework.menu', 'services',
> 'maps', 'services.entertainment', 'services.otherservices']
> Installed Middleware: ('django.middleware.common.CommonMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware')
>
>
> Traceback: File
> "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/core/handlers/base.py"
> in get_response
> 116. response = callback(request, *callback_args, **callback_kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/contrib/admin/options.py"
> in wrapper
> 370. return self.admin_site.admin_view(view)(*args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/utils/decorators.py"
> in _wrapped_view
> 91. response = view_func(request, *args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/cache.py"
> in _wrapped_view_func
> 89. response = view_func(request, *args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/contrib/admin/sites.py"
> in inner
> 202. return view(request, *args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/utils/decorators.py"
> in _wrapper
> 25. return bound_func(*args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/utils/decorators.py"
> in _wrapped_view
> 91. response = view_func(request, *args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/utils/decorators.py"
> in bound_func
> 21. return func(self, *args2, **kwargs2) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/transaction.py"
> in inner
> 208. return func(*args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/contrib/admin/options.py"
> in add_view
> 1045. self.save_related(request, form, formsets, False) File
> "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/contrib/admin/options.py"
> in save_related
> 762. self.save_formset(request, form, formset, change=change) File
> "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/contrib/admin/options.py"
> in save_formset
> 750. formset.save() File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/forms/models.py"
> in save
> 494. return self.save_existing_objects(commit) + self.save_new_objects(commit) File
> "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/forms/models.py"
> in save_new_objects
> 628. self.new_objects.append(self.save_new(form, commit=commit)) File
> "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/forms/models.py"
> in save_new
> 728. pk_value = getattr(self.instance, self.fk.rel.field_name) File
> "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/fields/related.py"
> in __get__
> 389. raise self.field.rel.to.DoesNotExist
>
> Exception Type: DoesNotExist at
> /admin/entertainment/entertainment/add/ Exception Value: