Re: Error - module 'django.db.models' has no attribute 'FieldDoesNotExist' when adding FK relationship object
Addendum: the Release model is declared as follows: from django.db import models from tinymce import models as tinymce_models from artists.models import Artist from bands.models import Band from labels.models import Label class Release(models.Model): artist = models.ForeignKey(Artist, on_delete=models.CASCADE) band = models.ForeignKey( Band, on_delete=models.CASCADE, blank=True, null=True ) title = models.CharField(max_length=200) label = models.ForeignKey(Label, on_delete=models.CASCADE) cat_no = models.CharField( max_length=200, blank=True, null=True, default='UNKNOWN' ) country = models.CharField( max_length=200, blank=True, null=True, default=None ) RELEASE_FORMAT_CHOICES = [ ('LP', 'LP'), ('10INCHALBUM', '10" ALBUM'), ('7INCHEP', '7" EP'), ('12INCHEP', '12" EP'), ('12INCHSINGLE', '12" SINGLE'), ('10INCHSINGLE', '10" SINGLE'), ('7INCHSINGLE', '7" SINGLE'), ] release_format = models.CharField( max_length=200, choices=RELEASE_FORMAT_CHOICES, blank=True, null=True, default=None ) year = models.PositiveSmallIntegerField() genre = models.CharField( max_length=200, blank=True, null=True, default=None ) bio = tinymce_models.HTMLField(blank=True, null=True) date_created = models.DateTimeField(auto_now_add=True) date_modified = models.DateTimeField(auto_now=True) class Meta: verbose_name = "release" verbose_name_plural = "releases" def __str__(self): return self.title I suspect that something is going on with migrations. All migrations have been run and a user is already capturing data on a staging server, so I do not want to recreate the database. Regards, ~ Lloyd On Sat, Aug 7, 2021 at 1:02 AM Lloyd Dube wrote: > Hi, > > I have two models, ReleaseImageGallery and ReleaseImage as follows: > > class ReleaseImageGallery(BaseImageGallery): > release = models.ForeignKey(Release, on_delete=models.CASCADE) > > class Meta: > verbose_name = "release image gallery" > verbose_name_plural = "release image galleries" > > and > > class ReleaseImage(BaseImage): > release_image_gallery = models.ForeignKey(ReleaseImageGallery, > on_delete=models.CASCADE) > image = models.ImageField(upload_to='release-images/', blank=True, null= > True) > class Meta: > verbose_name = "release image" > verbose_name_plural = "release images" > > > > My admin.py file is as follows: > from django.contrib import admin > > from .models import ReleaseImageGallery, ReleaseImage > > > class ReleaseImageInline(admin.StackedInline): > model = ReleaseImage > extra = 1 > > class ReleaseImageGalleryAdmin(admin.ModelAdmin): > inlines = [ReleaseImageInline] > list_filter = ['date_created', 'date_modified'] > search_fields = ['title'] > > > admin.site.register(ReleaseImageGallery, ReleaseImageGalleryAdmin) > > When I run the project and log into the admin site, all works fine. > > While creating a new ReleaseImageGallery, I try to add a new Release > object (FK relationship) via a select list, an error is raised in the > add_view as follows: > > [image: add-release-fk-object.png] > > ``` > Environment: > > > Request Method: GET > Request URL: > http://127.0.0.1:8000/admin/releases/release/add/?_to_field=id&_popup=1 > > Django Version: 3.2.5 > Python Version: 3.9.6 > Installed Applications: > ['django.contrib.admin', > 'django.contrib.auth', > 'django.contrib.contenttypes', > 'django.contrib.sessions', > 'django.contrib.messages', > 'django.contrib.staticfiles', > 'tinymce', > 'nested_inline', > 'coresite.apps.CoreSiteConfig', > 'customadminoptions.apps.CustomAdminOptionsConfig', > 'baseimagegalleries.apps.BaseImageGalleriesConfig', > 'baseusers.apps.UsersConfig', > 'userprofiles.apps.UserProfilesConfig', > 'artists.apps.ArtistsConfig', > 'artistimagegalleries.apps.ArtistImageGalleriesConfig', > 'bands.apps.BandsConfig', > 'labels.apps.LabelsConfig', > 'releaseimagegalleries.apps.ReleaseImageGalleriesConfig', > 'releases.apps.ReleasesConfig'] > Installed Middleware: > ('whitenoise.middleware.WhiteNoiseMiddleware', > 'django.middleware.security.SecurityMiddleware', > 'django.contrib.sessions.middleware.SessionMiddleware', > 'django.middleware.common.CommonMiddleware', > 'django.middleware.csrf.CsrfViewMiddleware', > 'django.contrib.auth.middleware.AuthenticationMiddleware', > 'django.contrib.messages.middleware.MessageMiddleware', > 'django.middleware.clickjacking.XFrameOptionsMiddleware') > > > > Traceback (most recent call last): > File > "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/db/models/options.py", > line 608, in get_field > return self.fields_map[field_name] > > During handling of the above exception ('_to_field'), another exception > occurred: > File > "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/nested_inline/admin.py", > line 217, in add_view > f = opts.get_field(k) > File >
Error - module 'django.db.models' has no attribute 'FieldDoesNotExist' when adding FK relationship object
Hi, I have two models, ReleaseImageGallery and ReleaseImage as follows: class ReleaseImageGallery(BaseImageGallery): release = models.ForeignKey(Release, on_delete=models.CASCADE) class Meta: verbose_name = "release image gallery" verbose_name_plural = "release image galleries" and class ReleaseImage(BaseImage): release_image_gallery = models.ForeignKey(ReleaseImageGallery, on_delete=models.CASCADE) image = models.ImageField(upload_to='release-images/', blank=True, null=True ) class Meta: verbose_name = "release image" verbose_name_plural = "release images" My admin.py file is as follows: from django.contrib import admin from .models import ReleaseImageGallery, ReleaseImage class ReleaseImageInline(admin.StackedInline): model = ReleaseImage extra = 1 class ReleaseImageGalleryAdmin(admin.ModelAdmin): inlines = [ReleaseImageInline] list_filter = ['date_created', 'date_modified'] search_fields = ['title'] admin.site.register(ReleaseImageGallery, ReleaseImageGalleryAdmin) When I run the project and log into the admin site, all works fine. While creating a new ReleaseImageGallery, I try to add a new Release object (FK relationship) via a select list, an error is raised in the add_view as follows: [image: add-release-fk-object.png] ``` Environment: Request Method: GET Request URL: http://127.0.0.1:8000/admin/releases/release/add/?_to_field=id&_popup=1 Django Version: 3.2.5 Python Version: 3.9.6 Installed Applications: ['django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'tinymce', 'nested_inline', 'coresite.apps.CoreSiteConfig', 'customadminoptions.apps.CustomAdminOptionsConfig', 'baseimagegalleries.apps.BaseImageGalleriesConfig', 'baseusers.apps.UsersConfig', 'userprofiles.apps.UserProfilesConfig', 'artists.apps.ArtistsConfig', 'artistimagegalleries.apps.ArtistImageGalleriesConfig', 'bands.apps.BandsConfig', 'labels.apps.LabelsConfig', 'releaseimagegalleries.apps.ReleaseImageGalleriesConfig', 'releases.apps.ReleasesConfig'] Installed Middleware: ('whitenoise.middleware.WhiteNoiseMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware') Traceback (most recent call last): File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/db/models/options.py", line 608, in get_field return self.fields_map[field_name] During handling of the above exception ('_to_field'), another exception occurred: File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/nested_inline/admin.py", line 217, in add_view f = opts.get_field(k) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/db/models/options.py", line 610, in get_field raise FieldDoesNotExist("%s has no field named '%s'" % (self.object_name, field_name)) During handling of the above exception (Release has no field named '_to_field'), another exception occurred: File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/contrib/admin/options.py", line 616, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/utils/decorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/contrib/admin/sites.py", line 232, in inner return view(request, *args, **kwargs) File "/Users/sd/Library/Caches/pypoetry/virtualenvs/afridisc-sWjK8x-o-py3.9/lib/python3.9/site-packages/django/utils/decorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File