Ahmad Sidawi Ahmad Sidawi - 3 months ago 16
Python Question

'unicode' object has no attribute 'utcoffset'

In my admin, I am getting errors for only one class, 'unicode' object has no attribute 'utcoffset'. I have looked at a few other similar questions and have been unable to solve it. Any ideas on how to fix it? The traceback is below the class.

class PartRequest(models.Model):
pub_date = models.DateTimeField('date published', default = '2016-08-10', blank=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
part_request_number = models.CharField(_('Part Request Number'),max_length=10, default = number)
serialnumber = models.CharField(_('Serial Number'),max_length=10, default= snumber)
partnumber = models.CharField(_('Part Number'), max_length = 500, default = 'e.g. 002109_1')
build_type = models.ForeignKey(buildtyp, related_name='BuildType', null=True)
project_manager = models.ForeignKey(Person, related_name = 'Manager', null = True)
requester = models.ForeignKey(Person, related_name = 'Requester', null = True)
project_id = models.ForeignKey(Project, related_name = 'Project', null=True)
ordernumber = models.PositiveIntegerField(_('Order Number'), default=0)
description = models.CharField(_('Description'), max_length=500)
quantityrequired = models.PositiveIntegerField(_('Quantity'), default=0)
sensitivity = models.ForeignKey(sens, related_name='Sensitivity', null=True)
build_risk = models.ForeignKey(buildrisk, related_name= 'Risk', null=True)
daterequired = models.DateField(_('Date Required'), default = '2000-08-16')
image_or_pdf_upload = models.FileField(upload_to = upload_location, null=True, blank = True)
material = models.ForeignKey(mat, related_name = 'Material', null=True)
other_requirements = models.CharField(_('Other Requirements'), max_length = 100, default ='')
cert = models.CharField(_('Certificate of Conformity'), max_length=50, default ='')
location = models.ForeignKey(locat, related_name='Location', null=True)
identification_method = models.ForeignKey(MOI, related_name= 'MOI', null=True)
packing = models.CharField(_('Packing Specified by End User'),max_length=100, default = 'Please specify here')
qainfo = models.CharField(_('QA Information with Delivery'),max_length=100, default = 'Please specify here')
shipping = models.CharField(_('Shipping Details'),max_length=100, default = 'Please specify here')
slug = models.SlugField(unique=True, null=True)
def __unicode__(self):
return (self.part_request_number)

def get_absolute_url(self):
return "/buildpage/%s/" %(self.slug)

def create_slug(instance, new_slug=None):
slug = slugify(instance.part_request_number)
if new_slug is not None:
slug = new_slug
qs = PartRequest.objects.filter(slug=slug).order_by("-id")
exists = qs.exists()
if exists:
new_slug = "%s-%s" %(slug, qs.first().id)
return create_slug(instance, new_slug=new_slug)
return slug

def pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
instance.slug = create_slug(instance)
pre_save.connect(pre_save_receiver, sender = PartRequest)


Traceback:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/buildpage/partrequest/add/?_changelist_filters=requester__id__exact%3D2

Django Version: 1.9.2
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'prodman',
'video',
'tande',
'assets',
'buildpage',
'concept',
'smart_selects')
Installed Middleware:
('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',
'django.middleware.security.SecurityMiddleware')


Template error:
In template /Library/Python/2.7/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 33
'unicode' object has no attribute 'utcoffset' 23 : {% endblock %}
24 : {% endif %}
25 :
26 : {% block content %}<div id="content-main">
27 : {% block object-tools %}
28 : {% if change %}{% if not is_popup %}
29 : <ul class="object-tools">
30 : {% block object-tools-items %}
31 : <li>
32 : {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
33 : <a href="{% add_preserved_filters histo ry_url %}" class= "historylink">{% trans "History" %}</a>
34 : </li>
35 : {% if has_absolute_url %}<li><a href="{{ absolute_url }}" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif %}
36 : {% endblock %}
37 : </ul>
38 : {% endif %}{% endif %}
39 : {% endblock %}
40 : <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.model_name }}_form" novalidate>{% csrf_token %}{% block form_top %}{% endblock %}
41 : <div>
42 : {% if is_popup %}<input type="hidden" name="{{ is_popup_var }}" value="1" />{% endif %}
43 : {% if to_field %}<input type="hidden" name="{{ to_field_var }}" value="{{ to_field }}" />{% endif %}


Traceback:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
172. response = response.render()

File "/Library/Python/2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content

File "/Library/Python/2.7/site-packages/django/template/response.py" in rendered_content
137. content = template.render(context, self._request)

File "/Library/Python/2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
206. return self._render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
69. result = block.nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
69. result = block.nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
220. nodelist.append(node.render_annotated(context))

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
209. return template.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
208. return self._render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
220. nodelist.append(node.render_annotated(context))

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
220. nodelist.append(node.render_annotated(context))

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
326. return nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
326. return nodelist.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render
1049. return render_value_in_context(output, context)

File "/Library/Python/2.7/site-packages/django/template/base.py" in render_value_in_context
1026. value = force_text(value)

File "/Library/Python/2.7/site-packages/django/utils/encoding.py" in force_text
78. s = six.text_type(s)

File "/Library/Python/2.7/site-packages/django/utils/html.py" in <lambda>
381. klass.__unicode__ = lambda self: mark_safe(klass_unicode(self))

File "/Library/Python/2.7/site-packages/django/forms/boundfield.py" in __str__
43. return self.as_widget()

File "/Library/Python/2.7/site-packages/django/forms/boundfield.py" in as_widget
101. return force_text(widget.render(name, self.value(), attrs=attrs))

File "/Library/Python/2.7/site-packages/django/forms/widgets.py" in render
832. value = self.decompress(value)

File "/Library/Python/2.7/site-packages/django/forms/widgets.py" in decompress
904. value = to_current_timezone(value)

File "/Library/Python/2.7/site-packages/django/forms/utils.py" in to_current_timezone
190. if settings.USE_TZ and value is not None and timezone.is_aware(value):

File "/Library/Python/2.7/site-packages/django/utils/timezone.py" in is_aware
340. return value.utcoffset() is not None

Exception Type: AttributeError at /admin/buildpage/partrequest/add/
Exception Value: 'unicode' object has no attribute 'utcoffset'

Answer

The default value for your pub_date field is a string. It should be an instance of datetime.date.

Comments