Is there a best way to create a custom user model and extend to Profile for production in django? Because I keep getting these error in production

Viewed 219

Hi can anyone please highlight the best way to extend a custom user model to a profile or explain what I am doing wrong. I created a custom user model, and extended it to a profile app which all works fine and passes in development. However in production on heroku I kept running into server errors when I click on profile, so I turned debug to True and it saw this traceback;

ProgrammingError at /u/profile/
can't adapt type 'MyUser'

So I figured, hey probably it's complaining about passing in the whole model instead of the object in the model. However, the filter in my views specifies the object been retrieved be the object related to the requested user. Here's my view below:

User = get_user_model()

class ProfileUserView(View):
    def get(self, request, *args, **kwargs):
        if request.user.is_authenticated():
           user = get_object_or_404(User, username=request.user)
           post_list = Post.objects.filter(author__username__iexact=user).order_by('-submission_time')
           paginator = Paginator(post_list, 10)
           page = request.GET.get('page')

           try:
                post = paginator.page(page)
           except PageNotAnInteger:
                post = paginator.page(1)
           except EmptyPage:
                post = paginator.page(paginator.num_pages)
           profile = Profile.objects.get(user=request.user)
           template = 'profiles/profile_user.html'
           context = {
                    'profile': profile,
                    'post': post
                   }
           return render(request, template, context)
        else:
           return HttpResponseRedirect(reverse('account_login'))

Any help/explanation with regards to what I am doing wrong with retrieving associated data from the profile model by extending a custom user model will be greatly appreciated.

Here's the profile model block;

class Profile(models.Model):
    user = models.OneToOneField(User)
    image = models.ImageField(upload_to='images/', null=True, blank=True)
    rank = models.CharField(max_length=50, default="Beginner")

Here's the full traceback

Request Method: GET Request URL: https://sustenergy.herokuapp.com/u/profile/

Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in validate_number 41. number = int(number)

During handling of the above exception (int() argument must be a string, a bytes-like object or a number, not 'NoneType'), another exception occurred:

File "/app/profiles/views.py" in get 37. post = paginator.page(page)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in page 57. number = self.validate_number(number)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in validate_number 43. raise PageNotAnInteger(_('That page number is not an integer'))

During handling of the above exception (That page number is not an integer), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 65. return self.cursor.execute(sql, params)

The above exception (can't adapt type 'MyUser') was the direct cause of the following exception:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py" in view 68. return self.dispatch(request, *args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py" in dispatch 88. return handler(request, *args, **kwargs)

File "/app/profiles/views.py" in get 39. post = paginator.page(1)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in page 57. number = self.validate_number(number)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in validate_number 46. if number > self.num_pages:

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py" in get 35. res = instance.dict[self.name] = self.func(instance)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in num_pages 91. if self.count == 0 and not self.allow_empty_first_page:

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py" in get 35. res = instance.dict[self.name] = self.func(instance)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in count 79. return self.object_list.count()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in count 364. return self.query.get_count(using=self.db)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py" in get_count 499. number = obj.get_aggregation(using, ['__count'])['__count']

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py" in get_aggregation 480. result = compiler.execute_sql(SINGLE)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql 886. raise original_exception

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql 876. cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 80. return super(CursorDebugWrapper, self).execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 65. return self.cursor.execute(sql, params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py" in exit 94. six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/six.py" in reraise 685. raise value.with_traceback(tb)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute 65. return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /u/profile/ Exception Value: can't adapt type 'MyUser'

2 Answers
Related