1👍
✅
AbstractBaseUser
provides a get_username()
method which you can use instead. It practically does the same as what you’re doing: return getattr(self, self.USERNAME_FIELD)
.
class File(models.Model):
...
def clean(self):
#Check if path has a trailing '/'
if self.path[-1]!='/':
self.path = self.path+"/"
if self.filename[0]=='/':
self.filename = self.filename[1:]
#Get the full path
username = self.user.get_username()
self.path = "tau/"+username+"/"+self.path
The reason your original method failed is because get_user_model()
is executed when the module is first imported, at which time the app registry is not fully initialized. If you need to use get_user_model()
in a models.py
file, you should call it within a function or method, not at the module level:
class File(models.Model):
...
def clean(self):
User = get_user_model()
👤knbk
Source:stackexchange.com