0

I want to set profile's avatar, but I get an error while updating object: 'Profile' object has no attribute 'profile'

I've created a model:

models.py

class Profile(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='profile')
   slug = models.SlugField(editable=False, unique=True)
   avatar = models.ImageField(upload_to=get_upload_avatar, null=True, blank=True)

And Model Form:

froms.py

from PIL import Image

class AvatarForm(ModelForm):

    class Meta:
        model = Profile
        fields = ['avatar',]

    def save(self):
        avatar = super(AvatarForm, self).save()
        im = Image.open(avatar.avatar)
        resized_image = im.thumbnail(64, 64)
        resized_image.save(avatar.avatar.path)

        return avatar

Template rendering as usual:

template.html

<form action="{% url 'avatar_add' me.slug %}" enctype="multipart/form-data" method="POST">
    {% csrf_token %}
    {% render_field avatar_form.avatar class="form-control form-control-sm" accept="image/*" onchange="this.form.submit()"%}
</form>

And url reciever:

urls.py

url(r'^profile/(?P<slug>[-\w]+)/avatar_add$', AddAvatar, name='avatar_add'),

The handling view:

@login_required
def AddAvatar(request, slug):
    if request.method == 'POST' and request.FILES['avatar'] and request.user.profile.slug == slug:
        form_avatar = AvatarForm(request.POST, request.FILES)
        if form_avatar.is_valid():
            profile = Profile.objects.get(slug=slug)
            profile.avatar = form_avatar.cleaned_data['avatar']
            profile.save()   #  <--get err here
            return HttpResponse(form_avatar.cleaned_data['avatar'])

While saving profie I get error as above. Another problem is that def save() function will be never called, because I'm not saving ModalForm() instance, but just using it to update something other. I'm newbie in Django, please help.

Traceback:

File "C:\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python36-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "C:\Users\BA\DjangoProjects\szmatkaPL\core\views.py" in AddAvatar
  209.             profile.save()

File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in save
  806.                        force_update=force_update, update_fields=update_fields)

File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in save_base
  836.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in _save_table
  900.                       for f in non_pks]

File "C:\Python36-32\lib\site-packages\django\db\models\base.py" in <listcomp>
  900.                       for f in non_pks]

File "C:\Python36-32\lib\site-packages\django\db\models\fields\files.py" in pre_save
  296.             file.save(file.name, file.file, save=False)

File "C:\Python36-32\lib\site-packages\django\db\models\fields\files.py" in save
  93.         name = self.field.generate_filename(self.instance, name)

File "C:\Python36-32\lib\site-packages\django\db\models\fields\files.py" in generate_filename
  327.             filename = self.upload_to(instance, filename)

    File "C:\Users\BA\DjangoProjects\szmatkaPL\core\misc.py" in get_upload_avatar
  12.     return os.path.join('profile', 'images', 'avatar', instance.profile.user.username, filename)

Exception Type: AttributeError at /profile/xruchawicax/avatar_add
Exception Value: 'Profile' object has no attribute 'profile'
5
  • Show the full error and traceback. Where is this occurring? Do you have a custom save method on the model? Why have you defined one on the form since as you say you're never calling it? Commented Aug 19, 2017 at 12:01
  • I don't have custom save method on the model, err occures when I try to save profile. I know, I'm never calling it, I just wonder where I have to make thumbnail - in view or in forms.py Commented Aug 19, 2017 at 12:08
  • You still need to post the full traceback. Commented Aug 19, 2017 at 12:11
  • dpaste.com/3TREWDY Commented Aug 19, 2017 at 12:21
  • Well that is clearly showing that the error happens in get_upload_avatar. Now you need to show that code. Commented Aug 19, 2017 at 12:32

1 Answer 1

1

The error most probably is in your upload_to function "get_upload_avatar" in the model definition. It should be instance.user not instance.profile.user.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right, I'm a fool, thanks. I'm just copied this function from Image class, that has foreign key profile.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.