Julien Julien - 10 months ago 38
Python Question

Django - int() argument must be a string or a number, not 'Patients'

for the context :

  • I have 2 tables, user (which is a copy of the auth_user table, with small modifications) and patients

  • I want to make a DetailView without passing the pk argument, so my user don't have access to his id

  • The primary key of Patients is a the foreign key "patients" in User, so I can link these two tables

My Patients model:

class Patients(models.Model):
id = models.IntegerField(primary_key=True)
# And other keys not importants

My views.py:

class ProfilView(LoginRequiredMixin, generic.DetailView):
login_url = '/membres/login/'
#model = Patients
template_name = 'membres/profil.html'
def get_object(self):
user_loggin = get_object_or_404(User, pk=self.request.user.id)
return Patients.objects.get(pk=user_loggin.patients)

I think I return something wrong, but after searching on internet a lot, I don't understand how i'm suppose to make this working, thanks for your help.

Answer Source

Querying on the pk requires a integer object not a Patients object.

You should specify the id of the related patients object:

#                                           ^^^