oz grow oz grow - 1 year ago 133
Python Question

getattr - Exception Value: module 'django.db.models' has no attribute 'model_name''

I have a probleme for use a variable in model name, i want to take this command :

MyVar.objects.all().delete()


and in the same way i have to a probleme for take this :

class MyCsvModel(CsvDbModel):
class Meta:
dbModel = MyVar
delimiter = delimiter_csv


these actions are on methode of a class.
and variable is created on here methode.

Sorry for my english, i am beginner...

EDIT :

My code :

from etl.models import MvmtjExportTemps, MvmtjExport
from django.db import models

[...]
model_name = self.table_temporaire[self.numero_edi]
getattr(models, model_name).objects.all().delete()


class MyCsvModel(CsvDbModel):
class Meta:
dbModel = getattr(models, model_name)
delimiter = delimiter_csv


And i have this result :


Environment:

Request Method: GET Request URL:
http://127.0.0.1:8000/etl/integration/

Django Version: 1.8.18 Python Version: 3.6.1 Installed Applications:
['django.contrib.admin', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.staticfiles', 'pec',
'topo', 'bootstrap3_datetime', 'todo_app', 'debug_toolbar', 'bi',
'etl'] Installed Middleware:
['debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback: File
"C:\git\ATH_intranet\env_p3\lib\site-packages\django\core\handlers\base.py"
in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "c:\git\ATH_intranet\etl\views.py" in IntegrationExtraction
188. importation.ImportationTableTemporaire(fichier) File
"c:\git\ATH_intranet\etl\class_perso.py" in ImportationTableTemporaire
112. getattr(models, model_name).objects.all().delete()

Exception Type: AttributeError at /etl/integration/ Exception Value:
module 'django.db.models' has no attribute 'MvmtjExportTemps'


EDIT 2 :

Finaly i need add location in import and getattr like this :

import etl.models

#vidange table temporaire
model_name = self.table_temporaire[self.numero_edi]
getattr(etl.models, model_name).objects.all().delete()

# creation de la class correspondant a la table temporaire

class MyCsvModel(CsvDbModel):
class Meta:
dbModel = getattr(etl.models, model_name)
delimiter = delimiter_csv

Answer Source
import models 
model_name = "X"

getattr(models, model_name).objects.all().delete()

class MyCsvModel(CsvDbModel):
            class Meta:
                dbModel = getattr(models, model_name)
                delimiter = delimiter_csv
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download