1

In almost an identical setup to this question: Django FileField with upload_to determined at runtime

class Speaker(models.Model):
    user = models.OneToOneField(User, related_name='speaker')
    middle_name = models.CharField('Middle Name', max_length=40)
    profile_file = models.FileField(upload_to=profile_filename, blank=True, null=True)

def profile_filename(instance, filename):
    speaker = instance
    return "%s.%s.%s.%s.pdf" % (speaker.user.first_name, speaker.official_middle_initial, speaker.user.last_name, "profile")

I get an error: AttributeError: 'User' object has no attribute 'first_name'

This is an object that has long since been saved, and definitely has a user with values that is used everywhere else in the application. Speaker is the profile class for user. I feel like I've hit a bug here, no?

Additional Details

Here are some additional details. This is happening inside a celery task, so maybe that's an important factor. I pass the user to the celery task. It grabs the profile object associated with it using get_profile which is the speaker object. I save a file on the speaker object, and that's where we are.

When I list the fields on the user object they seem to be there:

[2012-08-19 11:52:40,819: WARNING/PoolWorker-5] ['date_joined', 'email', 'first_name', 'groups', 'id', 'is_active', 'is_staff', 'is_superuser', 'last_login', 'last_name', 'logentry', 'password', 'speaker', 'user_permissions', 'username', 'userpasswordresetrequired']

Here is the traceback:

[2012-08-19 11:52:40,834: ERROR/MainProcess] Task myapp.tasks.retrieveDocument[a4beb536-76a4-48ea-aaf9-9ca0668f9331] raised exception: 'User' object has no attribute 'first_name'
Traceback (most recent call last):
  File "/Users/bobspryn/lohi/Remedy/remedyenv/lib/python2.7/site-packages/celery/task/trace.py", line 212, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/Users/bobspryn/lohi/Remedy/myapp/tasks.py", line 24, in retrieveDocument
    afile.save(filename, content)
  File "/Users/bobspryn/lohi/Remedy/remedyenv/lib/python2.7/site-packages/django/db/models/fields/files.py", line 85, in save
    name = self.field.generate_filename(self.instance, name)
  File "/Users/bobspryn/lohi/Remedy/myapp/models.py", line 37, in w9_filename
    return "%s.%s.%s.%s.pdf" % (speaker.user.first_name, speaker.official_middle_initial, speaker.official_last_name, DOCUMENT_W9)
AttributeError: 'User' object has no attribute 'first_name'

Update

It does appear to have to do with it being executed inside of celery. Strange.

Update

Adding the task code.

@task()
def retrieveDocument(user, documentKey, documentType):
    echosign = EchoSign(user=user)
    fileData = echosign.getDocumentWithKey(documentKey)
    if not fileData:
        logger.error('Error retrieving document', exc_info=True)
    else:
        speaker = user.get_profile()
        filename = "%s.%s.%s.pdf" % (user.first_name, user.last_name, documentType)
        if documentType == DOCUMENT_TEST:
            afile = speaker.test_file
        elif documentType == DOCUMENT_PROFILE:
            afile = speaker.profile_file

        content = ContentFile(fileData)
        afile.save(filename, content)
        speaker.save()
Community
  • 1
  • 1
Bob Spryn
  • 17,742
  • 12
  • 68
  • 91
  • This should work. May be you are missing some information. Have you tried to look at `speaker`, `speaker.user` object attributes in that function though django's error page? – Rohan Aug 19 '12 at 08:37
  • Try to output all field names of 'User' object: print repr( speaker.user.__class__._meta.get_all_field_names() ) to view possible fields of User object. It can make something more clear. May be it is another User class (not from django.contrib.auth.models). – sergzach Aug 19 '12 at 09:38
  • Providing full traceback would be helpful – okm Aug 19 '12 at 10:06
  • Try printing `speaker.user` in the log. What does it return? – zsquare Aug 20 '12 at 13:51
  • Also, any chance that you added those fields after starting celery? – zsquare Aug 20 '12 at 13:52
  • Printing `speaker.user` errors out for the same reason. `__unicode__` for user tries to print the `first_name` attribute and pukes. – Bob Spryn Aug 20 '12 at 18:01

0 Answers0