2đź‘Ť
You have some serious problems with variable scope here. Just defining an attribute called “user_name” at the top of the class does not automatically give you access to it elsewhere in the class; you would need to access it via the class itself. Usually you do that through the self
variable that is the first parameter to every method.
However, many of your methods do not even accept a self
parameter, so they would give TypeError when they are called. On top of that, your user_name
attribute is actually a class attribute, which would be shared by all instances of User – this would clearly be a bad thing. You should really make it a Django field, like the other attributes.
Finally, your scope issues worsen when you try and access request
in one of those methods. Again, you can’t access a variable unless it has been passed to that method (or is available in global scope, which the request is definitely not). So get_username
cannot work at all.
I must say though that all that is irrelevant, as the error you get does not even match your code: you must have accessed Track.__user_name
somewhere to get that error.
0đź‘Ť
You do have a variable username, but its not a field which would mean that the query set it looks like you’re creating won’t find it
user_name = "no_user"
should be one of the following
user_name = models.CharField(default='no_user')
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True)
The only reason I’ve suggested a CharField
here is incase you don’t use some form of authorisation user model in your app. If you do, then you should use a foreign key to that model.
- [Answered ]-Using a class as a part of another class in django models
- [Answered ]-Persist Django FormView Data