Related
I am trying to open the template sem.html and year.html from my project, but it is showing an error unnecessarily. Earlier it will open the template smoothly but when I m trying with the foreign key it is giving an error that str returned a non-string value. I am joining my models.py file code where I created two models named sem and year and in forms.py I have created two forms semforms and yearforms with two fields.
class subjects(models.Model):
subject_code = models.CharField(max_length=20,null=True)
subject_name = models.CharField(max_length=100,null=True)
subject_abv = models.CharField(max_length=10,null=True)
semester = models.IntegerField(null=True)
theory_load = models.IntegerField(null=True)
max_numb_students = models.CharField(max_length=65)
faculty = models.ManyToManyField(facultyload)
def __str__(self):
return self.subject_code,self.subject_name
class semester(models.Model):
sem_num = models.CharField(max_length=10,null=True)
sem_courses = models.ManyToManyField(subjects,related_name='sem_courses')
#property
def get_courses(self):
return self.sem_courses
def __str__(self):
return self.sem_num
class Year(models.Model):
year_name = models.CharField(max_length=50)
courses = models.ManyToManyField(subjects,related_name='courses')
#property
def get_courses(self):
return self.courses
def __str__(self):
return self.year_name
forms.py
class semforms(forms.ModelForm):
class Meta:
model = semester
fields = ['sem_num','sem_courses']
class yearforms(forms.ModelForm):
class Meta:
model = Year
fields = ['year_name','courses']
It is giving this error
TypeError at /sem/
__str__ returned non-string (type tuple)
Request Method: GET
Request URL: http://127.0.0.1:8000/sem/
Django Version: 3.1.4
Exception Type: TypeError
Exception Value:
__str__ returned non-string (type tuple)
Anyone can solve this, please help.
views.py - (same for year function, instead of sem I have taken year)
#login_required(login_url='login')
#allowed_users(allowed_roles=['admin','customer'])
def sem(request):
if request.method == 'POST':
form = semforms(request.POST)
if form.is_valid():
try:
form.save()
return redirect('/sem')
except:
pass
else:
form = semforms()
sem = semester.objects.all()
total_sem = sem.count()
context = {'sem':sem,'total_sem':total_sem,'form':form}
return render(request,"sem.html",context)
#allowed_users(allowed_roles=['admin'])
def edit_sem(request, id):
sem = semester.objects.get(id=id)
return render(request,'editsem.html',{'sem':sem})
#allowed_users(allowed_roles=['admin'])
def update_sem(request, id):
sem = semester.objects.get(id=id)
form = semforms(request.POST,instance=sem)
if form.is_valid():
form.save()
messages.success(request,"Record updated successfully......")
return render(request,"editsem.html",{'sem':sem})
#allowed_users(allowed_roles=['admin'])
def delete_sem(request, id):
load = semester.objects.get(id=id)
load.delete()
return render(request,'sem.html',{'load':load})
Also, this is my full Traceback error.
Internal Server Error: /sem/
Traceback (most recent call last):
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\Virti Parekh\projects\final\total_load\decorators.py", line 22, in wrapper_func
return view_func(request, *args, **kwargs)
File "C:\Users\Virti Parekh\projects\final\total_load\views.py", line 462, in sem
return render(request,"sem.html",context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 170, in render
return self._render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 162, in _render
return self.nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 312, in render
return nodelist.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 938, in render
bit = node.render_annotated(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 905, in render_annotated
return self.render(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 988, in render
output = self.filter_expression.resolve(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 671, in resolve
obj = self.var.resolve(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 796, in resolve
value = self._resolve_lookup(context)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 858, in _resolve_lookup
current = current()
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\forms.py", line 297, in as_p
errors_on_separate_row=True,
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\forms.py", line 236, in _html_output
'field_name': bf.html_name,
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\html.py", line 376, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\boundfield.py", line 34, in __str__
return self.as_widget()
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\boundfield.py", line 97, in as_widget
renderer=self.form.renderer,
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 241, in render
context = self.get_context(name, value, attrs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 678, in get_context
context = super().get_context(name, value, attrs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 639, in get_context
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\widgets.py", line 587, in optgroups
for index, (option_value, option_label) in enumerate(self.choices):
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py", line 1157, in __iter__
yield self.choice(obj)
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py", line 1171, in choice
self.field.label_from_instance(obj),
File "C:\Users\Virti Parekh\AppData\Local\Programs\Python\Python37\lib\site-packages\django\forms\models.py", line 1240, in label_from_instance
return str(obj)
TypeError: __str__ returned non-string (type tuple)
Change your subjects class __str__ method to this:
class subjects(models.Model):
def __str__(self):
return f"{self.subject_code},{self.subject_name}"
I am trying to integrate amazon simple email service to my app. My app is a python3 flask app.
I am using boto3. I am currently getting an error when I try to send a raw email.
I have created a separate route where I call this class and obtain send the "SendEmail" method.
I have a sample data json that would have to be passed on in SendEmail in the comments
I have provided the log of the error below as well
class SES():
def __init__(self, **kwargs):
"""Create template emails that can be used in the code.
The template emails will be stored in the brokerportal
will then get used by the aws ses boto3 client
When sending template emails:
1. Check if the template is part of the listed templates
2. Call the necessary template to build the message
3. Send it to the parties using send email """
self.region = 'ap-south-1'
self.aws_access_key_id = current_app.config['ses_access_key_id'],
self.aws_secret_access_key = current_app.config['ses_secret_access_key']
self.ses = boto3.client('ses', region_name = self.region,
aws_access_key_id = self.aws_access_key_id,
aws_secret_access_key = self.aws_secret_access_key)
def SendEmail(self, data, **kwargs):
"""takes the data and transforms the data into the appropriate frmat
before sending the email
data should have the following:
data = {
'toList':['s#domain.com'],
'ccList':['b#domain.com'],
'bccList':['p#domain.com'],
'TemplateData':{vars},
'Attachment':True,
'fileKey':'<S3path>',
'bucketName': <Amazon s3 bucket name>
'Subject': 'This is a subject',
'TemplateName':'TemplateName'
}"""
CHARSET = "utf-8"
Destination = {
'ToAddresses':data['toList'],
'CcAddresses':data['ccList'],
'BccAddresses':data['bccList']
}
# ReplyToAddresses = [current_app.config['MAIL_USERNAME']],
ReplyToAddresses = ["a#domain.com"],
msg = MIMEMultipart('mixed')
msg['Subject'] = data['Subject']
msg['From'] = "a#domain.com"
if Destination['ToAddresses']:
msg['To'] = ', '.join(str(v) for v in Destination['ToAddresses'])
if Destination['CcAddresses']:
msg['Cc'] = ', '.join(str(v) for v in Destination['CcAddresses'])
if Destination['BccAddresses']:
msg['Bcc'] = ', '.join(str(v) for v in Destination['BccAddresses'])
msg_body = MIMEMultipart('alternative')
#Obtain the template details
# v = current_app.config['db'].view('_design/Email/_view/templates')
v = current_app.config['db'].view('_design/Email/_view/templates')
for row in v[data['TemplateName']]:
Tmplte = Jinja2Template(row.value['HtmlPart'])
HtmlPart = Tmplte.render(data = data['TemplateData'])
htmlpart = MIMEText(HtmlPart)
msg_body.attach(htmlpart)
msg.attach(msg_body)
#Attachment
if data['Attachment']:
filehelper = FileHelper(bucketName=data['bucketName'])
att = MIMEApplication(filehelper.load(data['fileKey']))
att.add_header('Content-Disposition','attachment',filename=fileKey)
msg.attach(att)
else:
pass
try:
current_app.logger.debug(msg)
print(msg)
response = self.ses.send_raw_email(
RawMessage = {
'Data': msg.as_string()
})
return msg
except ClientError as e:
current_app.logger.debug(e.response['Error']['Message'])
return ""
Error:
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs))) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e) File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs))) File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb) File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 33, in reraise
raise value File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request() File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args) File "/home/sunil/perilwise/brokerportalbe/main.py", line 522, in sesmailcheck
response = ses.SendEmail(data = data) File "/home/sunil/perilwise/brokerportalbe/repository/sesRepository.py", line 117, in SendEmail
'Data': msg.as_string() File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 312, in _api_call
return self._make_api_call(operation_name, kwargs) File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 592, in _make_api_call
operation_model, request_dict) File "/usr/local/lib/python3.6/dist-packages/botocore/endpoint.py", line 141, in make_request
return self._send_request(request_dict, operation_model) File "/usr/local/lib/python3.6/dist-packages/botocore/endpoint.py", line 166, in _send_request
request = self.create_request(request_dict, operation_model) File "/usr/local/lib/python3.6/dist-packages/botocore/endpoint.py", line 150, in create_request
operation_name=operation_model.name) File "/usr/local/lib/python3.6/dist-packages/botocore/hooks.py", line 227, in emit
return self._emit(event_name, kwargs) File "/usr/local/lib/python3.6/dist-packages/botocore/hooks.py", line 210, in _emit
response = handler(**kwargs) File "/usr/local/lib/python3.6/dist-packages/botocore/signers.py", line 90, in handler
return self.sign(operation_name, request) File "/usr/local/lib/python3.6/dist-packages/botocore/signers.py", line 154, in sign
auth.add_auth(request) File "/usr/local/lib/python3.6/dist-packages/botocore/auth.py", line 366, in add_auth
self._inject_signature_to_request(request, signature) File "/usr/local/lib/python3.6/dist-packages/botocore/auth.py", line 369, in _inject_signature_to_request
l = ['AWS4-HMAC-SHA256 Credential=%s' % self.scope(request)] File "/usr/local/lib/python3.6/dist-packages/botocore/auth.py", line 319, in scope
return '/'.join(scope) TypeError: sequence item 0: expected str instance, tuple found
I solved this by making the SES() like a module that can be called from main.py (i've given the code below):
class SES(object):
def __init__(self, app = None):
"""Create template emails that can be used in the code.
The template emails will be stored in the brokerportal
will then get used by the aws ses boto3 client
When sending template emails:
1. Check if the template is part of the listed templates
2. Call the necessary template to build the message
3. Send it to the parties using send email """
if app is not None:
self.init_app(app)
def init_app(self, app):
self.region = app.config.get('ap-south-1')
self.aws_access_key_id = app.config.get('ses_access_key_id')
self.aws_secret_access_key = app.config.get('ses_secret_access_key')
self.ses = self._connect()
def _connect():
clt = boto3.client('ses', region_name = self.region,
aws_access_key_id = self.aws_access_key_id,
aws_secret_access_key = self.aws_secret_access_key)
return clt
def SendEmail(self, data, **kwargs):
"""takes the data and transforms the data into the appropriate frmat
before sending the email
data should have the following:
data = {
'toList':['s#domain.com'],
'ccList':['b#domain.com'],
'bccList':['p#domain.com'],
'TemplateData':{vars},
'Attachment':True,
'fileKey':'<S3path>',
'bucketName': <Amazon s3 bucket name>
'Subject': 'This is a subject',
'TemplateName':'TemplateName'
}"""
CHARSET = "utf-8"
Destination = {
'ToAddresses':data['toList'],
'CcAddresses':data['ccList'],
'BccAddresses':data['bccList']
}
# ReplyToAddresses = [current_app.config['MAIL_USERNAME']],
ReplyToAddresses = ["a#domain.com"],
msg = MIMEMultipart('mixed')
msg['Subject'] = data['Subject']
msg['From'] = "a#domain.com"
if Destination['ToAddresses']:
msg['To'] = ', '.join(str(v) for v in Destination['ToAddresses'])
if Destination['CcAddresses']:
msg['Cc'] = ', '.join(str(v) for v in Destination['CcAddresses'])
if Destination['BccAddresses']:
msg['Bcc'] = ', '.join(str(v) for v in Destination['BccAddresses'])
msg_body = MIMEMultipart('alternative')
#Obtain the template details
# v = current_app.config['db'].view('_design/Email/_view/templates')
v = current_app.config['db'].view('_design/Email/_view/templates')
for row in v[data['TemplateName']]:
Tmplte = Jinja2Template(row.value['HtmlPart'])
HtmlPart = Tmplte.render(data = data['TemplateData'])
htmlpart = MIMEText(HtmlPart)
msg_body.attach(htmlpart)
msg.attach(msg_body)
#Attachment
if data['Attachment']:
filehelper = FileHelper(bucketName=data['bucketName'])
att = MIMEApplication(filehelper.load(data['fileKey']))
att.add_header('Content-Disposition','attachment',filename=fileKey)
msg.attach(att)
else:
pass
try:
current_app.logger.debug(msg)
print(msg)
response = self.ses.send_raw_email(
RawMessage = {
'Data': msg.as_string()
})
return msg
except ClientError as e:
current_app.logger.debug(e.response['Error']['Message'])
return ""
I then called ses = SES(app) in the main.py route and called ses like a function
This solved my problem
I've this piece of code to add files to a zip in Django. Works fine when media is local but when using an s3 bucket I get the following error:
FileNotFoundError at /en/admin/certification/application/
[Errno 2] No such file or directory: 'https://bucket-name.s3.amazonaws.com/media/application_files/name_of_the_pdf.pdf'
Here's the code:
filename = settings.MEDIA_ROOT + q.user.username + '-' + q.certification.standard.name + '-' + q.certification.name
zf = zipfile.ZipFile(byte, "w")
zipped_files = []
zip_filename = filename + '.zip'
csv_filename = filename + '.csv'
with open(csv_filename, 'w') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
for answer in q.answer_set.all():
if answer.question.question_type in ['file', 'files']:
if answer.f :
if 'RDS_HOSTNAME' in os.environ:
res = answer.f.url
else: res = settings.BASE_DIR + answer.f.url
zf.write(res)
elif answer.question.question_type == 'text' : res = answer.text
else : res = answer.checkbox.name
filewriter.writerow([answer.question.question, res])
zf.write(csv_filename)
zf.close()
resp = HttpResponse(
byte.getvalue(), content_type="application/x-zip-compressed")
resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename
And the traceback:
Traceback:
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
604. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
45. return bound_method(*args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in changelist_view
1701. response = self.response_action(request, queryset=cl.get_queryset(request))
File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/contrib/admin/options.py" in response_action
1400. response = func(self, request, queryset)
File "/opt/python/current/app/certification/admin_actions.py" in download_application
31. zf.write(res)
File "/usr/lib64/python3.6/zipfile.py" in write
1617. zinfo = ZipInfo.from_file(filename, arcname)
File "/usr/lib64/python3.6/zipfile.py" in from_file
507. st = os.stat(filename)
Exception Type: FileNotFoundError at /en/admin/certification/application/
Exception Value: [Errno 2] No such file or directory: 'https://bucket-name.s3.amazonaws.com/media/application_files/name_of_the_pdf.pdf'
I have changed the bucket and file names to examples.
What have I got wrong?
This is how I resolved it:
if answer.f :
if 'RDS_HOSTNAME' in os.environ:
res = answer.f.url
response = requests.get(answer.f.url)
zf.writestr(answer.f.url, response.content)
else:
res = settings.BASE_DIR + answer.f.url
zf.write(res)
i'm using odoo 8 and i have develop a custom module for biometric device and it shows error
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
KeyError: 'employee_id'
i tried to fix the problem but it always display the same problem. Any idea for help please ?
Traceback
Traceback (most recent call last):
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 544, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 581, in dispatch
result = self._call_function(**self.params)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 317, in _call_function
return checked_call(self.db, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\service\model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 314, in checked_call
return self.endpoint(*a, **kw)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 810, in __call__
return self.method(*args, **kw)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\http.py", line 410, in response_wrap
response = f(*args, **kw)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\web\controllers\main.py", line 948, in call_button
action = self._call_kw(model, method, args, {})
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\web\controllers\main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 268, in wrapper
return old_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_data_wizard.py", line 22, in import_attendance
biometric_attendance.crate_attendance_in_openep()
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_data_wizard.py", line 36, in crate_attendance_in_openep
biometric_machine, biometric_data_obj, biometric_user_obj,)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_data.py", line 145, in import_data_classmethod
attendances = biometric_machine.getattendance()
File "C:\Python2.7.13\lib\site-packages\mock.py", line 1201, in patched
return func(*args, **keywargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_machine.py", line 136, in getattendance
self.create_user()
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_machine.py", line 119, in create_user
'biometric_device': self.id, }
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\api.py", line 266, in wrapper
return new_api(self, *args, **kwargs)
File "D:\odoo_8.0.20170129\odoo-8.0-20170129\openerp\addons\hr_biometric_machine\models\biometric_user.py", line 33, in create
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
KeyError: 'employee_id'
biometric_user.py
class BiometricUser(models.Model):
_name = 'biometric.user'
biometric_id = fields.Integer('Id in biometric device')
name = fields.Char('Name in biometric device')
employee_id = fields.Many2one('hr.employee', 'Related employee')
biometric_device = fields.Many2one(
'biometric.machine', 'Biometric device', )
#api.model
def create(self, values):
userExist = None
biometric_machine_obj = self.env['biometric.machine']
biometric_user_obj = self.env['biometric.user']
userExist = biometric_user_obj.search([('biometric_id', '=', int(values['biometric_id'])),
('biometric_device', '=', int(values['biometric_device']))])
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
('biometric_device', '=', int(values['biometric_device']))])
print(len(userExist))
if len(userExist) != 0:
raise Warning(_('User exist in ZK machine'))
else:
if len(userHasUserDevice) != 0:
raise Warning(_('Every employee doesn\'t have more than one user in a specific biometric device !'))
else:
openerp_machine = biometric_machine_obj.search([('id', '=', values['biometric_device']), ], )
print ("openerp_machine:", openerp_machine['ip_address'])
print ("biometric_device:", values['biometric_device'])
conn = None
zk = ZK(str(openerp_machine['ip_address']), port=int(openerp_machine['port']), timeout=5)
try:
print 'Connecting to device ...'
conn = zk.connect()
print 'Disabling device ...'
conn.disable_device()
conn.set_user(uid=int(str(values['biometric_id'])), name=str(values['name']),
privilege=const.USER_DEFAULT, password='', group_id='',
user_id=str(values['biometric_id']))
print 'Enabling device ...'
conn.enable_device()
res_id = super(BiometricUser, self).create(values)
except Exception, e:
print "Process terminate : {}".format(e)
raise Warning(_('Something bad happened. ' + str(e)))
finally:
if conn:
conn.disconnect()
return res_id
Please check values of create() method, employee_id is not available and you are trying to get employee_id from values.
You should check that employee_id is available in values or not as like following :
if 'employee_id' in values :
userHasUserDevice = biometric_user_obj.search([('employee_id', '=', int(values['employee_id'])),
('biometric_device', '=', int(values['biometric_device']))])
I want to import data from a csv file to a model. So I have this code which does the job for me:
Import.py
import csv
with open('usuarios.csv') as f:
reader = csv.reader(f)
for row in reader:
created = Users.objects.create(
field1=row[0],
field2=row[1],
field3=row[2],
)
And my model:
class Users(AbstractBaseUser):
field1 = models.CharField(max_length=100, null=True)
field2 = models.CharField(max_length=100, null=True)
field3 = models.CharField(max_length=100, null=True)
But for some reason, when I execute import.ty the console handles this:
File "<console>", line 1, in <module>
File "<string>", line 25, in <module>
File "C:\Python34\lib\site-packages\django\db\models\manager.py", line 154, in
get_or_create
return self.get_queryset().get_or_create(**kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 376, in g
et_or_create
return self.get(**lookup), False
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 301, in g
et
clone = self.filter(*args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 593, in f
ilter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\db\models\query.py", line 611, in _
filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py", line 1204,
in add_q
clause = self._add_q(where_part, used_aliases)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py", line 1240,
in _add_q
current_negated=current_negated)
File "C:\Python34\lib\site-packages\django\db\models\sql\query.py", line 1131,
in build_filter
clause.add(constraint, AND)
File "C:\Python34\lib\site-packages\django\utils\tree.py", line 104, in add
data = self._prepare_data(data)
File "C:\Python34\lib\site-packages\django\db\models\sql\where.py", line 79, i
n _prepare_data
value = obj.prepare(lookup_type, value)
File "C:\Python34\lib\site-packages\django\db\models\sql\where.py", line 352,
in prepare
return self.field.get_prep_lookup(lookup_type, value)
File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
1085, in get_prep_lookup
return super(IntegerField, self).get_prep_lookup(lookup_type, value)
File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
369, in get_prep_lookup
return self.get_prep_value(value)
File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", line
1079, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: '(3482) 453874'
Ideas ? I've been reading that it could have something to be with the primary_key, but I dont know what exactly
Try this:
import csv
with open('usuarios.csv') as f:
reader = csv.reader(f)
for row in reader:
kwargs = {field1=row[0],
field2=row[1],
field3=row[2],}
created = Users(**kwargs)
created.save()