0

I've upgraded a django project to the latest version 3.1.2 from an older version and one issue is Meta Classes imports. I have this model which intends to inherit meta classes like so

class Guide(Contact):
    operator = models.ForeignKey('guides.SafariOperator', blank=True, null=True, on_delete=models.CASCADE)
    level = models.ForeignKey('guides.MembershipLevel', blank=True, null=True, on_delete=models.CASCADE)
    expiry_date = models.DateField(default=django.utils.timezone.now)
    language = models.ManyToManyField('guides.Language', blank=True)
    
    class Meta:
        ordering = ['name']

class GuideResource(resources.ModelResource):
    id = fields.Field(column_name='Bd/Sr', attribute="id") 
    sortorder = fields.Field(column_name='Bd/Sr', attribute="sortorder")
    #description = fields.Field(column_name='Company', attribute="description")
    address = fields.Field(column_name='Wk PO Box', attribute="address")
    telephone = fields.Field(column_name='Tel', attribute="telephone")
    fax = fields.Field(column_name='Fax', attribute="fax")
    email = fields.Field(column_name='E-mail', attribute="email")
    

    class Meta(Guide.Meta):
        abstract = True

which then throws this error

Traceback (most recent call last):
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/core/handlers/base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
File "/home/sammy/webapps/envs/kpsga/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 233, in inner
    return view(request, *args, **kwargs)
File "/home/sammy/webapps/kpsga/import_export/admin.py", line 154, in import_action
    context['fields'] = [f.column_name for f in resource.get_fields()]
File "/home/sammy/webapps/kpsga/import_export/resources.py", line 113, in get_fields
    return [self.fields[f] for f in self.get_export_order()]
File "/home/sammy/webapps/kpsga/import_export/resources.py", line 296, in get_export_order
    return self._meta.export_order or self.fields.keys()

Exception Type: AttributeError at /admin/guides/guide/import/
Exception Value: 'GuideResource' object has no attribute '_meta'

when clicking a custom admin button for importing data.

Sam B.
  • 2,703
  • 8
  • 40
  • 78
  • 1
    I think maybe you meant `class Meta(Guide.Meta):` ? (missing the `.Meta`). I would have also expected to see `abstract = True` on the `Guide.Meta` class. Can you double check against the [reference](https://docs.djangoproject.com/en/3.1/topics/db/models/#meta-inheritance) and let me know if that's what you're looking for? – sytech Nov 18 '20 at 07:33
  • @sytech your right, i made the changes but it still throws the same error – Sam B. Nov 18 '20 at 07:51
  • Hmm. Only thing off the top of my head is that I think you have to run migrations after changing ordering Meta options. Though, I'm not sure that forgetting this step would cause the error you're seeing here. – sytech Nov 18 '20 at 07:55
  • @sytech I did a makemigration then migrate and restarted gunicorn, same error – Sam B. Nov 18 '20 at 07:56
  • Reading the updated question... I didn't mean to suggest `abstract=True` belonged on the `GuideResource` Meta definition. Perhaps try removing that bit... unless `GuideResource` is, in fact, an abstract model. Looking even closer it seems you're using a django library import-export. Looking at the [docs](https://django-import-export.readthedocs.io/en/latest/getting_started.html) it doesn't seem the Meta is intended to be defined this way. I think you don't want any inheritence at all and you want to use `model=Guide` and then `export_order=...`. – sytech Nov 18 '20 at 08:02

0 Answers0