Friday, November 30, 2018

Why isn't user.photo being saved in UpdateView?

For some reason, the updateview will save all of the other user fields (and update them if changed) to the database.  Do I need to handle the user.photo field differently? 

# views.py
class UserUpdateView(LoginRequiredMixin, UpdateView):

    model
= User
    fields
= ["name", "photo", "city", "experience", "domain", "bio"]

   
def get_success_url(self):
       
return reverse("users:detail", kwargs={"username": self.request.user.username})

   
def get_object(self):
       
return User.objects.get(username=self.request.user.username)

user_update_view
= UserUpdateView.as_view()


The form:
# form.py
from django.contrib.auth import get_user_model, forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _


User = get_user_model()

class UserChangeForm(forms.UserChangeForm):

   
class Meta(forms.UserChangeForm.Meta):
        model
= User




It does save the photo though in the Admin.
# admin.py
from
django.contrib import admin
from django.contrib.auth import admin as auth_admin
from django.contrib.auth import get_user_model

from lexpy.users.forms import UserChangeForm, UserCreationForm

User = get_user_model()

@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):


    form
= UserChangeForm
    add_form
= UserCreationForm
    fieldsets
= (("User", {"fields": ("name", "photo", "height_field", "width_field",
           
"city", "experience", "domain", "bio")}),) + auth_admin.UserAdmin.fieldsets
    list_display
= ["username", "name", "is_superuser"]
    search_fields
= ["name"]
Enter code here...


My Models:

# models.py
from datetime import date


from django.contrib.auth.models import AbstractUser
from django.db import models
from django.urls import reverse
from django.utils.translation import ugettext_lazy as _


def user_directory_path(instance, filename):
   
#file will be uploaded to MEDIA_ROOT/user_<id>/<filename>
   
return 'user_{0}/{1}'.format(instance.username, filename)


class User(AbstractUser):
    EXPERIENCE_BEGINNER
= 'B'
    EXPERIENCE_BEGINT
= 'BI'
    EXPERIENCE_INTERMEDIATE
= 'I'
    EXPERIENCE_INTERVANCED
= 'IA'
    EXPERIENCE_ADVANCED
= 'A'
    DOMAIN_WEB_DEVELOPMENT
= 'WD'
    DOMAIN_SCIENTIFIC_AND_NUMERIC
= 'SN'
    DOMAIN_EDUCATION
= 'E'
    DOMAIN_DESKTOP_GUIS
= 'DG'
    DOMAIN_GAME_DEVELOPMENT
= 'GD'
    DOMAIN_SOFTWARE_DEVELOPMENT
= 'SD'
    DOMAIN_SCRIPTING
= 'S'


    EXPERIENCE_CHOICES
= (
       
(EXPERIENCE_BEGINNER, 'Beginner'),
       
(EXPERIENCE_BEGINT, 'Beginner to Intermediate'),
       
(EXPERIENCE_INTERMEDIATE, 'Intermediate'),
       
(EXPERIENCE_INTERVANCED, 'Intermediate to Advanced'),
       
(EXPERIENCE_ADVANCED, 'Advanced / Professional'),
       
)


    DOMAIN_CHOICES
= (
       
(DOMAIN_WEB_DEVELOPMENT, "Web Applications"),
       
(DOMAIN_SCIENTIFIC_AND_NUMERIC, "Scientific or Numeric Applications"),
       
(DOMAIN_EDUCATION, "Education"),
       
(DOMAIN_DESKTOP_GUIS, "Desktop Software (GUI's)"),
       
(DOMAIN_GAME_DEVELOPMENT, "Game Development"),
       
(DOMAIN_SOFTWARE_DEVELOPMENT, "Support Software"),
       
(DOMAIN_SCRIPTING, "Scripting"),
       
)


   
# First Name and Last Name do not cover name patterns
   
# around the globe.
    name
= models.CharField(_("Name of User"), blank=True, max_length=255)
    photo
= models.ImageField(upload_to=user_directory_path,
            blank
=True,
            width_field
="width_field",
            height_field
="height_field")
    height_field
= models.IntegerField(default=0)
    width_field
= models.IntegerField(default=0)
    city
= models.CharField(max_length=50, blank=True)
   
# Fix default
    experience
= models.CharField(max_length=2, choices=EXPERIENCE_CHOICES, blank=True, null=True)
    domain
= models.CharField(max_length=2, choices=DOMAIN_CHOICES, blank=True, null=True)
    bio
= models.TextField(max_length=500, blank=True)


   
def get_absolute_url(self):
       
return reverse("users:detail", kwargs={"username": self.username})






 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9a0c73a2-3e4c-41b8-b905-bb75639a16cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment