AlliDeacon
AlliDeacon

Reputation: 1495

Get Data from Inline with Admin.User Model

I am trying to use an 'augmentation' table with the Admin.User Model to store additional data related to that user/login.

Here is my admin.py

class UserProfileInline(admin.StackedInline):
    model = AppUserProfile
    can_delete = False
    verbose_name_plural = 'profile'


# Define a new User admin
class UserAdmin(BaseUserAdmin):
    inlines = (UserProfileInline,)


# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)

Here is my models.py

class AppUserProfile(models.Model):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    usr_tpid = models.CharField(max_length=20)
    usr_cstid = models.CharField(max_length=20)
    db_table = 'app_user_profile'

Here is my views.py where I'm trying to get the usr_cstid from the login value.

def login_view(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            print(request.POST)
            username = form.cleaned_data['userName']
            password = form.cleaned_data['password']
            userInfo = AppUserProfile.objects.all().filter(user=username)
            print(username)
            print(userInfo)
            user = authenticate(username=username, password=password)
            if user is not None:
                login(request, user)
                groups = [x.name for x in user.groups.all()]
                full_info = User.objects.all().filter(username=username)
                user_info = User.objects.all().filter(username=username).values('first_name')
                print(full_info)
                request.session['first_name'] = user_info[0]['first_name']
                request.session['user_group'] = groups[0]
                # print(user_info, groups)
                # return render(request, template_name='landingPage.html', context={'user': first_name})
                if 'Administrator' in groups:
                    # return render(request, template_name='adm_landing_page.html', context={'user': first_name})
                    return redirect('AdminPortal:adm_landing_page')
                if 'Supplier' in groups:
                    # return HttpResponseRedirect(reverse('adm_landing_page', args=(), kwargs={'user': username}))
                    return render(request, template_name='supplier_landing_page.html', )
                    # return redirect('SupplierPortal:supplier_landing_page')

            else:
                form = LoginForm()
    else:
        form = LoginForm()

    return render(request, 'registration/login.html', {'form': form})

However I'm getting the following error:

ValueError: invalid literal for int() with base 10: 'supplier_ad'

It seems this is the line causing the error:

    userInfo = AppUserProfile.objects.all().filter(user=username)

I am not sure what I have wrong, or how to link back to those values.

Upvotes: 0

Views: 284

Answers (1)

iklinac
iklinac

Reputation: 15738

Not sure what are you trying to achieve with this but you cannot filter user object by username string rather by user object or users id or you could do something like this

userInfo = AppUserProfile.objects.all().filter(user__username=username)

If you are expecting one AppUserProfile instance use get() instead of filter()

Also I am not seeing what exactly you are trying to achieve as when you login your user it is always accessible with request.user and depending on relation you could get resulting profile for instance assuming OneToOne relationship request.user.profile

Upvotes: 1

Related Questions