ValueError: Related model 'user.UserProfile' cannot be resolved Django 2.1










0















I am trying to create a UserProfile model in my user app and instead of inheriting from models.Model, I am inheriting from django's own User model so that I would have a custom user model wwith extra fields. But when I run the python manage.py test command it gives me the following error:



 Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 604, in run_tests
old_config = self.setup_databases()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 551, in setup_databases
self.parallel, **kwargs
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestutils.py", line 174, in setup_databases
serialize=connection.settings_dict.get('TEST', ).get('SERIALIZE', True),
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbasecreation.py", line 68, in create_test_db
run_syncdb=True,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 148, in call_command
return command.execute(*args, **defaults)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandsmigrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsmigration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsoperationsfields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendssqlite3schema.py", line 127, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbaseschema.py", line 495, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 966, in db_parameters
return "type": self.db_type(connection), "check": self.db_check(connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 963, in db_type
return self.target_field.rel_db_type(connection=connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 878, in target_field
return self.foreign_related_fields[0]
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 632, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 619, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 604, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'user.UserProfile' cannot be resolved


Here is what my models.py look like.



class UserProfile(User):
levels = [
('Beginner', 'Beginner'),
('Elementary', 'Elementary'),
('Pre-Intermediate', 'Pre-Intermediate'),
('Intermediate', 'Intermediate'),
('Upper-Intermediate', 'Upper-Intermediate'),
('Advanced', 'Advanced'),
]

courses = [
('English', 'English'),
('Russian', 'Russian'),
('Mathematics', 'Mathematics'),

]

titles = [
('Teacher', 'Teacher'),
('Student', 'Student'),
]

icons = [
('fa-user', 'User'),
('fa-user-graduate', 'Student'),
('fa-user-ninja', 'Ninja'),
('fa-user-tie', 'Tie'),
('fa-user-md', 'Doctor'),
('fa-user-astronaut', 'Astronaut'),
('fa-user-secret', 'Spy'),
('fa-user-injured', 'Injured'),
]

bio = models.TextField(blank=True)
country = models.CharField(max_length = 100)
course = models.CharField(
max_length = 20,
choices=courses,
default='English',
)
level = models.CharField(
max_length = 20,
choices=levels,
default='Beginner',
)
title = models.CharField(
max_length = 20,
choices=titles,
default='Student',
)
avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
icon = models.CharField(
max_length = 150,
choices = icons,
default = 'fa-user'
)


def is_student(self):
return self.title == 'Student'


def is_teacher(self):
return self.title == 'Teacher'

def get_absolute_url(self):
return reverse('user-profile', kwargs='slug': self.username)

def __str__(self):
return f'self.username Profile'

def save(self, **kwargs):
super().save()

img = Image.open(self.avatar.path)

if img.height != img.width:
w = img.width
h = img.height

if h > w:
a = h - w
box = (a/2, a/2, w, w)
else:
a = w - h
box = (a/2, a/2, h, h)

img = img.crop(box)

if img.height > 500 or img.width > 500:
output_size = (500, 500)
img.thumbnail(output_size)
img.save(self.avatar.path)
else:
img.save(self.avatar.path)


And I have added it in settings.py










share|improve this question
























  • Can you show your model you're trying to use?

    – Jon Clements
    Nov 10 '18 at 19:11











  • You don't seem to have shown the code. Where are you defining UserProfile? Is it in an app called "user"? Is that "user" app added to INSTALLED_APPS?

    – Daniel Roseman
    Nov 10 '18 at 19:11











  • Can you please share your model(s)? It looks like you refered to 'user.UserProfile' but likely the app user, or the model in that app UserProfile does not exists.

    – Willem Van Onsem
    Nov 10 '18 at 19:11











  • @JonClements There I edited

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:14











  • @DanielRoseman Yes I have added in INSTALLED_APPS and the app's name is user

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:15
















0















I am trying to create a UserProfile model in my user app and instead of inheriting from models.Model, I am inheriting from django's own User model so that I would have a custom user model wwith extra fields. But when I run the python manage.py test command it gives me the following error:



 Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 604, in run_tests
old_config = self.setup_databases()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 551, in setup_databases
self.parallel, **kwargs
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestutils.py", line 174, in setup_databases
serialize=connection.settings_dict.get('TEST', ).get('SERIALIZE', True),
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbasecreation.py", line 68, in create_test_db
run_syncdb=True,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 148, in call_command
return command.execute(*args, **defaults)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandsmigrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsmigration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsoperationsfields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendssqlite3schema.py", line 127, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbaseschema.py", line 495, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 966, in db_parameters
return "type": self.db_type(connection), "check": self.db_check(connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 963, in db_type
return self.target_field.rel_db_type(connection=connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 878, in target_field
return self.foreign_related_fields[0]
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 632, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 619, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 604, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'user.UserProfile' cannot be resolved


Here is what my models.py look like.



class UserProfile(User):
levels = [
('Beginner', 'Beginner'),
('Elementary', 'Elementary'),
('Pre-Intermediate', 'Pre-Intermediate'),
('Intermediate', 'Intermediate'),
('Upper-Intermediate', 'Upper-Intermediate'),
('Advanced', 'Advanced'),
]

courses = [
('English', 'English'),
('Russian', 'Russian'),
('Mathematics', 'Mathematics'),

]

titles = [
('Teacher', 'Teacher'),
('Student', 'Student'),
]

icons = [
('fa-user', 'User'),
('fa-user-graduate', 'Student'),
('fa-user-ninja', 'Ninja'),
('fa-user-tie', 'Tie'),
('fa-user-md', 'Doctor'),
('fa-user-astronaut', 'Astronaut'),
('fa-user-secret', 'Spy'),
('fa-user-injured', 'Injured'),
]

bio = models.TextField(blank=True)
country = models.CharField(max_length = 100)
course = models.CharField(
max_length = 20,
choices=courses,
default='English',
)
level = models.CharField(
max_length = 20,
choices=levels,
default='Beginner',
)
title = models.CharField(
max_length = 20,
choices=titles,
default='Student',
)
avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
icon = models.CharField(
max_length = 150,
choices = icons,
default = 'fa-user'
)


def is_student(self):
return self.title == 'Student'


def is_teacher(self):
return self.title == 'Teacher'

def get_absolute_url(self):
return reverse('user-profile', kwargs='slug': self.username)

def __str__(self):
return f'self.username Profile'

def save(self, **kwargs):
super().save()

img = Image.open(self.avatar.path)

if img.height != img.width:
w = img.width
h = img.height

if h > w:
a = h - w
box = (a/2, a/2, w, w)
else:
a = w - h
box = (a/2, a/2, h, h)

img = img.crop(box)

if img.height > 500 or img.width > 500:
output_size = (500, 500)
img.thumbnail(output_size)
img.save(self.avatar.path)
else:
img.save(self.avatar.path)


And I have added it in settings.py










share|improve this question
























  • Can you show your model you're trying to use?

    – Jon Clements
    Nov 10 '18 at 19:11











  • You don't seem to have shown the code. Where are you defining UserProfile? Is it in an app called "user"? Is that "user" app added to INSTALLED_APPS?

    – Daniel Roseman
    Nov 10 '18 at 19:11











  • Can you please share your model(s)? It looks like you refered to 'user.UserProfile' but likely the app user, or the model in that app UserProfile does not exists.

    – Willem Van Onsem
    Nov 10 '18 at 19:11











  • @JonClements There I edited

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:14











  • @DanielRoseman Yes I have added in INSTALLED_APPS and the app's name is user

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:15














0












0








0








I am trying to create a UserProfile model in my user app and instead of inheriting from models.Model, I am inheriting from django's own User model so that I would have a custom user model wwith extra fields. But when I run the python manage.py test command it gives me the following error:



 Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 604, in run_tests
old_config = self.setup_databases()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 551, in setup_databases
self.parallel, **kwargs
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestutils.py", line 174, in setup_databases
serialize=connection.settings_dict.get('TEST', ).get('SERIALIZE', True),
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbasecreation.py", line 68, in create_test_db
run_syncdb=True,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 148, in call_command
return command.execute(*args, **defaults)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandsmigrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsmigration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsoperationsfields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendssqlite3schema.py", line 127, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbaseschema.py", line 495, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 966, in db_parameters
return "type": self.db_type(connection), "check": self.db_check(connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 963, in db_type
return self.target_field.rel_db_type(connection=connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 878, in target_field
return self.foreign_related_fields[0]
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 632, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 619, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 604, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'user.UserProfile' cannot be resolved


Here is what my models.py look like.



class UserProfile(User):
levels = [
('Beginner', 'Beginner'),
('Elementary', 'Elementary'),
('Pre-Intermediate', 'Pre-Intermediate'),
('Intermediate', 'Intermediate'),
('Upper-Intermediate', 'Upper-Intermediate'),
('Advanced', 'Advanced'),
]

courses = [
('English', 'English'),
('Russian', 'Russian'),
('Mathematics', 'Mathematics'),

]

titles = [
('Teacher', 'Teacher'),
('Student', 'Student'),
]

icons = [
('fa-user', 'User'),
('fa-user-graduate', 'Student'),
('fa-user-ninja', 'Ninja'),
('fa-user-tie', 'Tie'),
('fa-user-md', 'Doctor'),
('fa-user-astronaut', 'Astronaut'),
('fa-user-secret', 'Spy'),
('fa-user-injured', 'Injured'),
]

bio = models.TextField(blank=True)
country = models.CharField(max_length = 100)
course = models.CharField(
max_length = 20,
choices=courses,
default='English',
)
level = models.CharField(
max_length = 20,
choices=levels,
default='Beginner',
)
title = models.CharField(
max_length = 20,
choices=titles,
default='Student',
)
avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
icon = models.CharField(
max_length = 150,
choices = icons,
default = 'fa-user'
)


def is_student(self):
return self.title == 'Student'


def is_teacher(self):
return self.title == 'Teacher'

def get_absolute_url(self):
return reverse('user-profile', kwargs='slug': self.username)

def __str__(self):
return f'self.username Profile'

def save(self, **kwargs):
super().save()

img = Image.open(self.avatar.path)

if img.height != img.width:
w = img.width
h = img.height

if h > w:
a = h - w
box = (a/2, a/2, w, w)
else:
a = w - h
box = (a/2, a/2, h, h)

img = img.crop(box)

if img.height > 500 or img.width > 500:
output_size = (500, 500)
img.thumbnail(output_size)
img.save(self.avatar.path)
else:
img.save(self.avatar.path)


And I have added it in settings.py










share|improve this question
















I am trying to create a UserProfile model in my user app and instead of inheriting from models.Model, I am inheriting from django's own User model so that I would have a custom user model wwith extra fields. But when I run the python manage.py test command it gives me the following error:



 Creating test database for alias 'default'...
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 26, in run_from_argv
super().run_from_argv(argv)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandstest.py", line 56, in handle
failures = test_runner.run_tests(test_labels)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 604, in run_tests
old_config = self.setup_databases()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestrunner.py", line 551, in setup_databases
self.parallel, **kwargs
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangotestutils.py", line 174, in setup_databases
serialize=connection.settings_dict.get('TEST', ).get('SERIALIZE', True),
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbasecreation.py", line 68, in create_test_db
run_syncdb=True,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagement__init__.py", line 148, in call_command
return command.execute(*args, **defaults)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 353, in execute
output = self.handle(*args, **options)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementbase.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangocoremanagementcommandsmigrate.py", line 203, in handle
fake_initial=fake_initial,
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsexecutor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsmigration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmigrationsoperationsfields.py", line 216, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendssqlite3schema.py", line 127, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbbackendsbaseschema.py", line 495, in alter_field
new_db_params = new_field.db_parameters(connection=self.connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 966, in db_parameters
return "type": self.db_type(connection), "check": self.db_check(connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 963, in db_type
return self.target_field.rel_db_type(connection=connection)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 878, in target_field
return self.foreign_related_fields[0]
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 632, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 619, in related_fields
self._related_fields = self.resolve_related_fields()
File "C:UsersUserAppDataLocalProgramslibsite-packagesdjangodbmodelsfieldsrelated.py", line 604, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'user.UserProfile' cannot be resolved


Here is what my models.py look like.



class UserProfile(User):
levels = [
('Beginner', 'Beginner'),
('Elementary', 'Elementary'),
('Pre-Intermediate', 'Pre-Intermediate'),
('Intermediate', 'Intermediate'),
('Upper-Intermediate', 'Upper-Intermediate'),
('Advanced', 'Advanced'),
]

courses = [
('English', 'English'),
('Russian', 'Russian'),
('Mathematics', 'Mathematics'),

]

titles = [
('Teacher', 'Teacher'),
('Student', 'Student'),
]

icons = [
('fa-user', 'User'),
('fa-user-graduate', 'Student'),
('fa-user-ninja', 'Ninja'),
('fa-user-tie', 'Tie'),
('fa-user-md', 'Doctor'),
('fa-user-astronaut', 'Astronaut'),
('fa-user-secret', 'Spy'),
('fa-user-injured', 'Injured'),
]

bio = models.TextField(blank=True)
country = models.CharField(max_length = 100)
course = models.CharField(
max_length = 20,
choices=courses,
default='English',
)
level = models.CharField(
max_length = 20,
choices=levels,
default='Beginner',
)
title = models.CharField(
max_length = 20,
choices=titles,
default='Student',
)
avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
icon = models.CharField(
max_length = 150,
choices = icons,
default = 'fa-user'
)


def is_student(self):
return self.title == 'Student'


def is_teacher(self):
return self.title == 'Teacher'

def get_absolute_url(self):
return reverse('user-profile', kwargs='slug': self.username)

def __str__(self):
return f'self.username Profile'

def save(self, **kwargs):
super().save()

img = Image.open(self.avatar.path)

if img.height != img.width:
w = img.width
h = img.height

if h > w:
a = h - w
box = (a/2, a/2, w, w)
else:
a = w - h
box = (a/2, a/2, h, h)

img = img.crop(box)

if img.height > 500 or img.width > 500:
output_size = (500, 500)
img.thumbnail(output_size)
img.save(self.avatar.path)
else:
img.save(self.avatar.path)


And I have added it in settings.py







python django python-3.x django-models






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 '18 at 19:13







Shosalim Baxtiyorov

















asked Nov 10 '18 at 19:09









Shosalim BaxtiyorovShosalim Baxtiyorov

187




187












  • Can you show your model you're trying to use?

    – Jon Clements
    Nov 10 '18 at 19:11











  • You don't seem to have shown the code. Where are you defining UserProfile? Is it in an app called "user"? Is that "user" app added to INSTALLED_APPS?

    – Daniel Roseman
    Nov 10 '18 at 19:11











  • Can you please share your model(s)? It looks like you refered to 'user.UserProfile' but likely the app user, or the model in that app UserProfile does not exists.

    – Willem Van Onsem
    Nov 10 '18 at 19:11











  • @JonClements There I edited

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:14











  • @DanielRoseman Yes I have added in INSTALLED_APPS and the app's name is user

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:15


















  • Can you show your model you're trying to use?

    – Jon Clements
    Nov 10 '18 at 19:11











  • You don't seem to have shown the code. Where are you defining UserProfile? Is it in an app called "user"? Is that "user" app added to INSTALLED_APPS?

    – Daniel Roseman
    Nov 10 '18 at 19:11











  • Can you please share your model(s)? It looks like you refered to 'user.UserProfile' but likely the app user, or the model in that app UserProfile does not exists.

    – Willem Van Onsem
    Nov 10 '18 at 19:11











  • @JonClements There I edited

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:14











  • @DanielRoseman Yes I have added in INSTALLED_APPS and the app's name is user

    – Shosalim Baxtiyorov
    Nov 10 '18 at 19:15

















Can you show your model you're trying to use?

– Jon Clements
Nov 10 '18 at 19:11





Can you show your model you're trying to use?

– Jon Clements
Nov 10 '18 at 19:11













You don't seem to have shown the code. Where are you defining UserProfile? Is it in an app called "user"? Is that "user" app added to INSTALLED_APPS?

– Daniel Roseman
Nov 10 '18 at 19:11





You don't seem to have shown the code. Where are you defining UserProfile? Is it in an app called "user"? Is that "user" app added to INSTALLED_APPS?

– Daniel Roseman
Nov 10 '18 at 19:11













Can you please share your model(s)? It looks like you refered to 'user.UserProfile' but likely the app user, or the model in that app UserProfile does not exists.

– Willem Van Onsem
Nov 10 '18 at 19:11





Can you please share your model(s)? It looks like you refered to 'user.UserProfile' but likely the app user, or the model in that app UserProfile does not exists.

– Willem Van Onsem
Nov 10 '18 at 19:11













@JonClements There I edited

– Shosalim Baxtiyorov
Nov 10 '18 at 19:14





@JonClements There I edited

– Shosalim Baxtiyorov
Nov 10 '18 at 19:14













@DanielRoseman Yes I have added in INSTALLED_APPS and the app's name is user

– Shosalim Baxtiyorov
Nov 10 '18 at 19:15






@DanielRoseman Yes I have added in INSTALLED_APPS and the app's name is user

– Shosalim Baxtiyorov
Nov 10 '18 at 19:15













1 Answer
1






active

oldest

votes


















0














I found what the problem was about!!!



It was because I was using my UserProfile model from user app as a ForeignKey on another model called Post as an author and the problem was there. Importing the model from another app directly and passing it there gives such error as it turns out. So following the documentation here https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey



I created another model called User in my another app and inherited from my UserProfile and here is how my jollywood/models.py looks:



 from django.db import models
from django.utils import timezone
from user.models import UserProfile


class Post(models.Model):
title = models.CharField(max_length = 150)
auth = models.ForeignKey('User', on_delete=models.CASCADE)
content_shown = models.TextField()
content_hidden = models.TextField()
date = models.DateTimeField(default=timezone.now)
read = models.PositiveIntegerField()
liked = models.PositiveIntegerField()
thumbnail = models.ImageField(default='post_thumbs/default_thumb.jpg', upload_to='post_thumbs')


def __str__(self):
return self.title

class User(UserProfile):
pass


and here is what my user/models.py looks like:



from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django.urls import reverse


class UserProfile(User):
levels = [
('Beginner', 'Beginner'),
('Elementary', 'Elementary'),
('Pre-Intermediate', 'Pre-Intermediate'),
('Intermediate', 'Intermediate'),
('Upper-Intermediate', 'Upper-Intermediate'),
('Advanced', 'Advanced'),
]

courses = [
('English', 'English'),
('Russian', 'Russian'),
('Mathematics', 'Mathematics'),

]

titles = [
('Teacher', 'Teacher'),
('Student', 'Student'),
]

icons = [
('fa-user', 'User'),
('fa-user-graduate', 'Student'),
('fa-user-ninja', 'Ninja'),
('fa-user-tie', 'Tie'),
('fa-user-md', 'Doctor'),
('fa-user-astronaut', 'Astronaut'),
('fa-user-secret', 'Spy'),
('fa-user-injured', 'Injured'),
]

bio = models.TextField(blank=True)
country = models.CharField(max_length = 100)
course = models.CharField(
max_length = 20,
choices=courses,
default='English',
)
level = models.CharField(
max_length = 20,
choices=levels,
default='Beginner',
)
title = models.CharField(
max_length = 20,
choices=titles,
default='Student',
)
avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
icon = models.CharField(
max_length = 150,
choices = icons,
default = 'fa-user'
)
instagram = models.CharField(max_length = 100, null=True)
facebook = models.CharField(max_length = 100, null=True)
twitter = models.CharField(max_length = 100, null=True)
telegram = models.CharField(max_length = 100, null=True)

def is_student(self):
return self.title == 'Student'


def is_teacher(self):
return self.title == 'Teacher'

def get_absolute_url(self):
return reverse('user-profile', kwargs='slug': self.username)

def __str__(self):
return f'self.username Profile'

def save(self, **kwargs):
super().save()

img = Image.open(self.avatar.path)

if img.height != img.width:
w = img.width
h = img.height

if h > w:
a = h - w
box = (a/2, a/2, w, w)
else:
a = w - h
box = (a/2, a/2, h, h)

img = img.crop(box)

if img.height > 500 or img.width > 500:
output_size = (500, 500)
img.thumbnail(output_size)
img.save(self.avatar.path)
else:
img.save(self.avatar.path)





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242482%2fvalueerror-related-model-user-userprofile-cannot-be-resolved-django-2-1%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I found what the problem was about!!!



    It was because I was using my UserProfile model from user app as a ForeignKey on another model called Post as an author and the problem was there. Importing the model from another app directly and passing it there gives such error as it turns out. So following the documentation here https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey



    I created another model called User in my another app and inherited from my UserProfile and here is how my jollywood/models.py looks:



     from django.db import models
    from django.utils import timezone
    from user.models import UserProfile


    class Post(models.Model):
    title = models.CharField(max_length = 150)
    auth = models.ForeignKey('User', on_delete=models.CASCADE)
    content_shown = models.TextField()
    content_hidden = models.TextField()
    date = models.DateTimeField(default=timezone.now)
    read = models.PositiveIntegerField()
    liked = models.PositiveIntegerField()
    thumbnail = models.ImageField(default='post_thumbs/default_thumb.jpg', upload_to='post_thumbs')


    def __str__(self):
    return self.title

    class User(UserProfile):
    pass


    and here is what my user/models.py looks like:



    from django.db import models
    from django.contrib.auth.models import User
    from PIL import Image
    from django.urls import reverse


    class UserProfile(User):
    levels = [
    ('Beginner', 'Beginner'),
    ('Elementary', 'Elementary'),
    ('Pre-Intermediate', 'Pre-Intermediate'),
    ('Intermediate', 'Intermediate'),
    ('Upper-Intermediate', 'Upper-Intermediate'),
    ('Advanced', 'Advanced'),
    ]

    courses = [
    ('English', 'English'),
    ('Russian', 'Russian'),
    ('Mathematics', 'Mathematics'),

    ]

    titles = [
    ('Teacher', 'Teacher'),
    ('Student', 'Student'),
    ]

    icons = [
    ('fa-user', 'User'),
    ('fa-user-graduate', 'Student'),
    ('fa-user-ninja', 'Ninja'),
    ('fa-user-tie', 'Tie'),
    ('fa-user-md', 'Doctor'),
    ('fa-user-astronaut', 'Astronaut'),
    ('fa-user-secret', 'Spy'),
    ('fa-user-injured', 'Injured'),
    ]

    bio = models.TextField(blank=True)
    country = models.CharField(max_length = 100)
    course = models.CharField(
    max_length = 20,
    choices=courses,
    default='English',
    )
    level = models.CharField(
    max_length = 20,
    choices=levels,
    default='Beginner',
    )
    title = models.CharField(
    max_length = 20,
    choices=titles,
    default='Student',
    )
    avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
    icon = models.CharField(
    max_length = 150,
    choices = icons,
    default = 'fa-user'
    )
    instagram = models.CharField(max_length = 100, null=True)
    facebook = models.CharField(max_length = 100, null=True)
    twitter = models.CharField(max_length = 100, null=True)
    telegram = models.CharField(max_length = 100, null=True)

    def is_student(self):
    return self.title == 'Student'


    def is_teacher(self):
    return self.title == 'Teacher'

    def get_absolute_url(self):
    return reverse('user-profile', kwargs='slug': self.username)

    def __str__(self):
    return f'self.username Profile'

    def save(self, **kwargs):
    super().save()

    img = Image.open(self.avatar.path)

    if img.height != img.width:
    w = img.width
    h = img.height

    if h > w:
    a = h - w
    box = (a/2, a/2, w, w)
    else:
    a = w - h
    box = (a/2, a/2, h, h)

    img = img.crop(box)

    if img.height > 500 or img.width > 500:
    output_size = (500, 500)
    img.thumbnail(output_size)
    img.save(self.avatar.path)
    else:
    img.save(self.avatar.path)





    share|improve this answer



























      0














      I found what the problem was about!!!



      It was because I was using my UserProfile model from user app as a ForeignKey on another model called Post as an author and the problem was there. Importing the model from another app directly and passing it there gives such error as it turns out. So following the documentation here https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey



      I created another model called User in my another app and inherited from my UserProfile and here is how my jollywood/models.py looks:



       from django.db import models
      from django.utils import timezone
      from user.models import UserProfile


      class Post(models.Model):
      title = models.CharField(max_length = 150)
      auth = models.ForeignKey('User', on_delete=models.CASCADE)
      content_shown = models.TextField()
      content_hidden = models.TextField()
      date = models.DateTimeField(default=timezone.now)
      read = models.PositiveIntegerField()
      liked = models.PositiveIntegerField()
      thumbnail = models.ImageField(default='post_thumbs/default_thumb.jpg', upload_to='post_thumbs')


      def __str__(self):
      return self.title

      class User(UserProfile):
      pass


      and here is what my user/models.py looks like:



      from django.db import models
      from django.contrib.auth.models import User
      from PIL import Image
      from django.urls import reverse


      class UserProfile(User):
      levels = [
      ('Beginner', 'Beginner'),
      ('Elementary', 'Elementary'),
      ('Pre-Intermediate', 'Pre-Intermediate'),
      ('Intermediate', 'Intermediate'),
      ('Upper-Intermediate', 'Upper-Intermediate'),
      ('Advanced', 'Advanced'),
      ]

      courses = [
      ('English', 'English'),
      ('Russian', 'Russian'),
      ('Mathematics', 'Mathematics'),

      ]

      titles = [
      ('Teacher', 'Teacher'),
      ('Student', 'Student'),
      ]

      icons = [
      ('fa-user', 'User'),
      ('fa-user-graduate', 'Student'),
      ('fa-user-ninja', 'Ninja'),
      ('fa-user-tie', 'Tie'),
      ('fa-user-md', 'Doctor'),
      ('fa-user-astronaut', 'Astronaut'),
      ('fa-user-secret', 'Spy'),
      ('fa-user-injured', 'Injured'),
      ]

      bio = models.TextField(blank=True)
      country = models.CharField(max_length = 100)
      course = models.CharField(
      max_length = 20,
      choices=courses,
      default='English',
      )
      level = models.CharField(
      max_length = 20,
      choices=levels,
      default='Beginner',
      )
      title = models.CharField(
      max_length = 20,
      choices=titles,
      default='Student',
      )
      avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
      icon = models.CharField(
      max_length = 150,
      choices = icons,
      default = 'fa-user'
      )
      instagram = models.CharField(max_length = 100, null=True)
      facebook = models.CharField(max_length = 100, null=True)
      twitter = models.CharField(max_length = 100, null=True)
      telegram = models.CharField(max_length = 100, null=True)

      def is_student(self):
      return self.title == 'Student'


      def is_teacher(self):
      return self.title == 'Teacher'

      def get_absolute_url(self):
      return reverse('user-profile', kwargs='slug': self.username)

      def __str__(self):
      return f'self.username Profile'

      def save(self, **kwargs):
      super().save()

      img = Image.open(self.avatar.path)

      if img.height != img.width:
      w = img.width
      h = img.height

      if h > w:
      a = h - w
      box = (a/2, a/2, w, w)
      else:
      a = w - h
      box = (a/2, a/2, h, h)

      img = img.crop(box)

      if img.height > 500 or img.width > 500:
      output_size = (500, 500)
      img.thumbnail(output_size)
      img.save(self.avatar.path)
      else:
      img.save(self.avatar.path)





      share|improve this answer

























        0












        0








        0







        I found what the problem was about!!!



        It was because I was using my UserProfile model from user app as a ForeignKey on another model called Post as an author and the problem was there. Importing the model from another app directly and passing it there gives such error as it turns out. So following the documentation here https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey



        I created another model called User in my another app and inherited from my UserProfile and here is how my jollywood/models.py looks:



         from django.db import models
        from django.utils import timezone
        from user.models import UserProfile


        class Post(models.Model):
        title = models.CharField(max_length = 150)
        auth = models.ForeignKey('User', on_delete=models.CASCADE)
        content_shown = models.TextField()
        content_hidden = models.TextField()
        date = models.DateTimeField(default=timezone.now)
        read = models.PositiveIntegerField()
        liked = models.PositiveIntegerField()
        thumbnail = models.ImageField(default='post_thumbs/default_thumb.jpg', upload_to='post_thumbs')


        def __str__(self):
        return self.title

        class User(UserProfile):
        pass


        and here is what my user/models.py looks like:



        from django.db import models
        from django.contrib.auth.models import User
        from PIL import Image
        from django.urls import reverse


        class UserProfile(User):
        levels = [
        ('Beginner', 'Beginner'),
        ('Elementary', 'Elementary'),
        ('Pre-Intermediate', 'Pre-Intermediate'),
        ('Intermediate', 'Intermediate'),
        ('Upper-Intermediate', 'Upper-Intermediate'),
        ('Advanced', 'Advanced'),
        ]

        courses = [
        ('English', 'English'),
        ('Russian', 'Russian'),
        ('Mathematics', 'Mathematics'),

        ]

        titles = [
        ('Teacher', 'Teacher'),
        ('Student', 'Student'),
        ]

        icons = [
        ('fa-user', 'User'),
        ('fa-user-graduate', 'Student'),
        ('fa-user-ninja', 'Ninja'),
        ('fa-user-tie', 'Tie'),
        ('fa-user-md', 'Doctor'),
        ('fa-user-astronaut', 'Astronaut'),
        ('fa-user-secret', 'Spy'),
        ('fa-user-injured', 'Injured'),
        ]

        bio = models.TextField(blank=True)
        country = models.CharField(max_length = 100)
        course = models.CharField(
        max_length = 20,
        choices=courses,
        default='English',
        )
        level = models.CharField(
        max_length = 20,
        choices=levels,
        default='Beginner',
        )
        title = models.CharField(
        max_length = 20,
        choices=titles,
        default='Student',
        )
        avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
        icon = models.CharField(
        max_length = 150,
        choices = icons,
        default = 'fa-user'
        )
        instagram = models.CharField(max_length = 100, null=True)
        facebook = models.CharField(max_length = 100, null=True)
        twitter = models.CharField(max_length = 100, null=True)
        telegram = models.CharField(max_length = 100, null=True)

        def is_student(self):
        return self.title == 'Student'


        def is_teacher(self):
        return self.title == 'Teacher'

        def get_absolute_url(self):
        return reverse('user-profile', kwargs='slug': self.username)

        def __str__(self):
        return f'self.username Profile'

        def save(self, **kwargs):
        super().save()

        img = Image.open(self.avatar.path)

        if img.height != img.width:
        w = img.width
        h = img.height

        if h > w:
        a = h - w
        box = (a/2, a/2, w, w)
        else:
        a = w - h
        box = (a/2, a/2, h, h)

        img = img.crop(box)

        if img.height > 500 or img.width > 500:
        output_size = (500, 500)
        img.thumbnail(output_size)
        img.save(self.avatar.path)
        else:
        img.save(self.avatar.path)





        share|improve this answer













        I found what the problem was about!!!



        It was because I was using my UserProfile model from user app as a ForeignKey on another model called Post as an author and the problem was there. Importing the model from another app directly and passing it there gives such error as it turns out. So following the documentation here https://docs.djangoproject.com/en/2.1/ref/models/fields/#django.db.models.ForeignKey



        I created another model called User in my another app and inherited from my UserProfile and here is how my jollywood/models.py looks:



         from django.db import models
        from django.utils import timezone
        from user.models import UserProfile


        class Post(models.Model):
        title = models.CharField(max_length = 150)
        auth = models.ForeignKey('User', on_delete=models.CASCADE)
        content_shown = models.TextField()
        content_hidden = models.TextField()
        date = models.DateTimeField(default=timezone.now)
        read = models.PositiveIntegerField()
        liked = models.PositiveIntegerField()
        thumbnail = models.ImageField(default='post_thumbs/default_thumb.jpg', upload_to='post_thumbs')


        def __str__(self):
        return self.title

        class User(UserProfile):
        pass


        and here is what my user/models.py looks like:



        from django.db import models
        from django.contrib.auth.models import User
        from PIL import Image
        from django.urls import reverse


        class UserProfile(User):
        levels = [
        ('Beginner', 'Beginner'),
        ('Elementary', 'Elementary'),
        ('Pre-Intermediate', 'Pre-Intermediate'),
        ('Intermediate', 'Intermediate'),
        ('Upper-Intermediate', 'Upper-Intermediate'),
        ('Advanced', 'Advanced'),
        ]

        courses = [
        ('English', 'English'),
        ('Russian', 'Russian'),
        ('Mathematics', 'Mathematics'),

        ]

        titles = [
        ('Teacher', 'Teacher'),
        ('Student', 'Student'),
        ]

        icons = [
        ('fa-user', 'User'),
        ('fa-user-graduate', 'Student'),
        ('fa-user-ninja', 'Ninja'),
        ('fa-user-tie', 'Tie'),
        ('fa-user-md', 'Doctor'),
        ('fa-user-astronaut', 'Astronaut'),
        ('fa-user-secret', 'Spy'),
        ('fa-user-injured', 'Injured'),
        ]

        bio = models.TextField(blank=True)
        country = models.CharField(max_length = 100)
        course = models.CharField(
        max_length = 20,
        choices=courses,
        default='English',
        )
        level = models.CharField(
        max_length = 20,
        choices=levels,
        default='Beginner',
        )
        title = models.CharField(
        max_length = 20,
        choices=titles,
        default='Student',
        )
        avatar = models.ImageField(default='avatars/default_avatar.jpg', upload_to='avatars')
        icon = models.CharField(
        max_length = 150,
        choices = icons,
        default = 'fa-user'
        )
        instagram = models.CharField(max_length = 100, null=True)
        facebook = models.CharField(max_length = 100, null=True)
        twitter = models.CharField(max_length = 100, null=True)
        telegram = models.CharField(max_length = 100, null=True)

        def is_student(self):
        return self.title == 'Student'


        def is_teacher(self):
        return self.title == 'Teacher'

        def get_absolute_url(self):
        return reverse('user-profile', kwargs='slug': self.username)

        def __str__(self):
        return f'self.username Profile'

        def save(self, **kwargs):
        super().save()

        img = Image.open(self.avatar.path)

        if img.height != img.width:
        w = img.width
        h = img.height

        if h > w:
        a = h - w
        box = (a/2, a/2, w, w)
        else:
        a = w - h
        box = (a/2, a/2, h, h)

        img = img.crop(box)

        if img.height > 500 or img.width > 500:
        output_size = (500, 500)
        img.thumbnail(output_size)
        img.save(self.avatar.path)
        else:
        img.save(self.avatar.path)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 4:57









        Shosalim BaxtiyorovShosalim Baxtiyorov

        187




        187



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242482%2fvalueerror-related-model-user-userprofile-cannot-be-resolved-django-2-1%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Crossroads (UK TV series)

            ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế