我正在尝试根据默认的User对象的id创建一个编码的id字段,但收到以下错误(完整跟踪):
File f = new File(fileName);
Scanner s = new Scanner(f);
Map<String, Integer> counts =
new Map<String, Integer>();
while( s.hasNext() ){
String word = s.next();
if( !counts.containsKey( word ) )
counts.put( word, 1 );
else
counts.put( word,
counts.get(word) + 1 );
}
我的Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/default_user/Documents/pumac3/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Users/default_user/Documents/pumac3/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/Users/default_user/Documents/pumac3/venv/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/default_user/Documents/pumac3/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/Users/default_user/Documents/pumac3/venv/lib/python2.7/site-packages/django/apps/config.py", line 199, in import_models
self.models_module = import_module(models_module_name)
File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/default_user/Documents/pumac3/pumac3/registration/models.py", line 52, in <module>
class Coach(models.Model):
File "/Users/default_user/Documents/pumac3/pumac3/registration/models.py", line 61, in Coach
user_id = models.CharField(max_length=254, default=urlsafe_base64_encode(force_bytes(user.id)))
AttributeError: 'OneToOneField' object has no attribute 'id'
中的我的Coach模型如下所示:
models.py
我已经尝试过查看与我类似的其他问题,但是这个人指定了一个主键,因此Django没有创建默认的class Coach(models.Model):
user = models.OneToOneField(User, unique=True)
# Sets default user values
user.is_active = False
# For account activation and password resetting
user_timestamp = models.IntegerField(default=int(time.time()))
user_id = models.CharField(max_length=254, default=urlsafe_base64_encode(force_bytes(user.id)))
user_token = models.CharField(max_length=254, default=default_token_generator.make_token(user))
name = models.CharField(max_length=100)
phone_number = models.CharField(max_length=20)
class Meta:
verbose_name_plural = "Coaches"
ordering = ['name']
def __unicode__(self):
"""Returns the name of the coach if has name, otherwise returns email."""
if self.name:
return self.name
else:
return self.user.email
def update_token(self):
"""Updates the user_token and timestamp for password reset or invalid activation"""
self.user_timestamp = int(time.time())
self.user_token = default_token_generator.make_token(user)
self.save()
@staticmethod
def is_coach(user):
return hasattr(user, 'coach')
@staticmethod
def authorized(user):
"""Returns a queryset of Coach objects the user can view and modify."""
if hasattr(user, 'coach'):
# check that the user is a coach
return user.coach
@staticmethod
def authorized_organizations(user):
"""
Returns a queryset of organizations the user can view and modify.
"""
if hasattr(user, 'coach'):
return user.coach.organizations.all()
字段。但是,我不是在我的id
模型中执行此操作,即使这样,Django默认用户模型是否应该包含Coach
属性?
我怀疑问题是id
字段仅在实际创建id
的实例后才存在,因此我无法为User
创建值。在创建user_id
类的实例后,我是否应该在models.py
之外实例化此值?
答案 0 :(得分:1)
您无法在班级的顶级代码中使用实例数据。
在代码运行时(导入时间),根本就没有实例。
如果要使用计算数据初始化实例,请覆盖类的__init__
方法。