How can I get a Field Id from a Related Django Model

I am working on a Django project where and I want to get an ID of a Related model with a OneToOne attributed so I can edit the profile of the user with his related Profile but all I get in return is Field 'id' expected a number but got 'GANDE1'.

Here are my Models:

class Profile(models.Model):
    customer = models.OneToOneField(User, on_delete=models.CASCADE, null = True)
    surname = models.CharField(max_length=20, null=True)
    othernames = models.CharField(max_length=40, null=True)
    gender = models.CharField(max_length=6, choices=GENDER, blank=True, null=True)
    address = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=11, null=True)

image = models.ImageField(default='avatar.jpg', blank=False, null=False, upload_to ='profile_images', 


#Method to save Image
def save(self, *args, **kwargs): 
    super().save(*args, **kwargs)
    img =
#Check for Image Height and Width then resize it then save
    if img.height > 200 or img.width > 150:
        output_size = (150, 250)

def __str__(self):
    return f'{self.customer.username}-Profile'

class Account(models.Model):
    customer = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
    account_number = models.CharField(max_length=10, null=True)
    date = models.DateTimeField(auto_now_add=True, null=True) 

    def __str__(self):
        return f' {self.customer} - Account No: {self.account_number}'

Here is my Views:

def create_account(request): 
#Search Customer
    if searchForm.is_valid():
        #Value of search form
        value = searchForm.cleaned_data['value']
        #Filter Customer by Surname, Othernames , Account Number using Q Objects
        user_filter = Q(customer__exact = value) | Q(account_number__exact = value)
        #Apply the Customer Object Filter
        list_customers = Account.objects.filter(user_filter) 
        list_customers = Account.objects.all()
context = {
    return render(request, 'dashboard/customers.html', context)

Here is how I displayed list of accounts in my Template:

{% for customer in customers %} 

                <td>{{ forloop.counter }}</td>
                <td>{{ customer.account_number  }}</td> 

                {% if customer.customer.profile.surname == None %}

                <td> <a class="btn btn-danger" href=" {% url 'update-customer' %} ">Click to Enter Customer Personal Details.</a> </td>

                {% else %}
                <td>{{ customer.customer.profile.surname }} {{ customer.customer.profile.othernames }}</td>
                <td>{{ }}</td>

                <td><a class="btn btn-success btn-sm" href="{% url 'account-statement' %}">Statement</a></td>
                <td><a class="btn btn-danger btn-sm" href="{% url 'dashboard-witdrawal'  %}">Withdraw</a></td>

                <th scope="row"><a class="btn btn-success btn-sm" href="{% url 'create-deposit' %}">Deposit</a></th>
                {% endif %}
              {% endfor %}  

Here is my Customer Update View where I am having issues:

def update_customer_profile(request, pk):
#get logged in user
user = request.user
#check if logged in user is staff

    customer_user = User.objects.get(id=pk)
except User.DoesNotExist:
    return redirect('user-register')
    count_users = User.objects.count() 
    #Get the Customer User's Profile from the User above
    user_profile = Profile.objects.get(customer=customer_user.username)

Please, understand that I want to get the ID of a User and MATCH it with the one in his profile record so I can be able to edit his profile record. Thanks in anticipation for your kind answer.

If you already have the User object, then simply use the relation between them:

    customer_user = User.objects.get(id=pk)
except User.DoesNotExist:
    user_profile = customer_user.profile

You don't need to query database separately. It's exaclty what relations are for.

Back to Top