python - django - name error on variable in class -


i new django, please bare me.

i making first model, , i'm creating upload system uploads folder name of user uploading it.

for reason, error when try create object model:

nameerror @ /admin/tracks/track/add/ name '_track__user_name' not defined 

here's models.py

from django.core.exceptions import validationerror django.db import models django.core.files.images import get_image_dimensions  # create models here.   class track(models.model):      user_name = "no_user"      def get_username():         user_name = "no_user"         if request.user.is_authenticated():             user_name = request.user.username         else:             user_name = "delete"      def generate_user_folder_tracks(instance, filename):         return "uploads/users/%s/tracks/%s" % (user_name, filename)      def is_mp3(value):         if not value.name.endswith('.mp3'):             raise validationerror(u'you may upload mp3 files tracks!')      def generate_user_folder_art(instance, filename):         return "uploads/users/%s/art/%s" % (user_name, filename)      def is_square_png(self):         if not self.name.endswith('.png'):             raise validationerror("you may upload png files album art!")         else:             w, h = get_image_dimensions(self)             if not h == w:                 raise validationerror("this picture not square! picture must equally wide height.")             else:                 if not (h + w) >= 1000:                     raise validationerror("this picture small! minimum dimensions 500 500 pixels.")         return self       # variables      track_type_choices = [         ('org', 'original'),         ('rmx', 'remix'),         ('clb', 'collab'),         ('liv', 'live'),     ]      # model fields      name = models.charfield(max_length=100)         desc = models.textfield(max_length=7500)     track_type = models.charfield(max_length=3,                                  choices=track_type_choices,                                  default='org')      track_type_content = models.charfield(max_length=100,blank=true)     created = models.timefield(auto_now=true,auto_now_add=false)      upload = models.filefield(upload_to=generate_user_folder_tracks,validators=[is_mp3])     albumart = models.imagefield(upload_to=generate_user_folder_art,validators=[is_square_png]) 

as can see first line after class defined, there variable called "user_name", , when using upload functions, supposed use variable folder name.

i confused why throwing error, doing wrong?

you have serious problems variable scope here. defining attribute called "user_name" @ top of class not automatically give access elsewhere in class; need access via class itself. through self variable first parameter every method.

however, many of methods not accept self parameter, give typeerror when called. on top of that, user_name attribute class attribute, shared instances of user - bad thing. should make django field, other attributes.

finally, scope issues worsen when try , access request in 1 of methods. again, can't access variable unless has been passed method (or available in global scope, request not). get_username cannot work @ all.

i must though irrelevant, error not match code: must have accessed track.__user_name somewhere error.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -