Get request, has PK, how do I access the PK data with in the model

355 views
Skip to first unread message

Travis Pickle

unread,
Jan 25, 2018, 4:39:43 PM1/25/18
to Django users
Lets say I have a model called Users.

I request(get) http://www.google.com/user/id (pk)

The users has age but finds to find other users with in same range, as well as other items that might be different ages.
Lets say I want to find similar interest for this user in
 http://www.google.com/profiles/32years (don't care that is is not a date, just want to search on it based on getting the pk id of user)

Obviously both of the models would have a date field to associate with the users age. I am just trying to figure out how to capture the users age/date/whatever it may be, then setup another query set on different models based on users age/birthday.

This data would be used to show relativeness to year 1985, which is not a fk or pk.

But we didn't know the year, until we got the get request.

Andy

unread,
Jan 26, 2018, 6:06:47 AM1/26/18
to Django users

Travis have a look the very cool and complete django tutorial which explaining how you query models in part2.
Once you have create the admin views, watch closely at how the list filters work to get an idea of what you have to do. :)

Travis Pickle

unread,
Jan 27, 2018, 6:19:02 AM1/27/18
to Django users
Andy,

Do you have a link for this? Thanks

Matemática A3K

unread,
Jan 27, 2018, 3:47:05 PM1/27/18
to django...@googlegroups.com

On Sat, Jan 27, 2018 at 8:19 AM, Travis Pickle <travis...@gmail.com> wrote:
Andy,

Do you have a link for this? Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7bc86b9f-57d4-4a57-aa2e-bf6f1a163458%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Travis Pickle

unread,
Jan 28, 2018, 2:01:35 PM1/28/18
to Django users
I am using django 1.8.

I read though the docs, however, i not seeing how I can get the PK other attributes to use in model searching. 


On Saturday, January 27, 2018 at 3:47:05 PM UTC-5, Matemática A3K wrote:
On Sat, Jan 27, 2018 at 8:19 AM, Travis Pickle <travis...@gmail.com> wrote:
Andy,

Do you have a link for this? Thanks

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Daniel Roseman

unread,
Jan 29, 2018, 4:30:03 AM1/29/18
to Django users
On Sunday, 28 January 2018 19:01:35 UTC, Travis Pickle wrote:
I am using django 1.8.

I read though the docs, however, i not seeing how I can get the PK other attributes to use in model searching. 

As others have stated, this is *fully* covered in the tutorial, specifically in part 3. Potted answer, though: you capture it as a URL argument and accept it in your view function. If you've read that part of the tutorial and still can't work it out, you'll need to post some actual code.
-- 
DR.

Travis Pickle

unread,
Jan 29, 2018, 8:30:06 AM1/29/18
to Django users

I'm assuming not a single person has read what is in my body of my message and has only read the subject. I know bots have read the body.

if
http://somesite.com/user/1 ,where 1 is the pk,
i want to use in the view pk relation to gather all fields, and use fields to generate other query sets to pass to the templates

user model has

name
birthday
gender

and car model has
make
model
year
description
and

profiles just have a one-to-one relationship to users
favorite_car
location
..etc

so for example, this isn't data isn't what i am working on, but it gets me to where i need to. I want to lookup(get_objects) where cars age is same as user PK 1, and also want to look up other users around same age, and report location.


Cant really disclose the code of this project at this time. but just note, I am trying to look for objects based on values from PK1. if I knew how to assign them, and then use it for a search it would have different questions.

Andréas Kühne

unread,
Jan 29, 2018, 8:40:13 AM1/29/18
to django...@googlegroups.com
Hi,

It's really hard to understand what you exactly want.

Without showing your code it'll be even harder to understand how / what you want to do. We have to guess from the little information you are giving.... However. if you want to query cars with the same year as the user for example, you could do this:

user = User.objects.get(pk=1)
cars = Car.objects.filter(year=user.birthday.year).all()

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Travis Pickle

unread,
Jan 29, 2018, 6:06:52 PM1/29/18
to Django users
Hiya --

Thanks. This is sort of what I was looking for.

user = User.objects.get(pk=pk)
cars = Car.objects.filter(year=user.birthday.year).all()
UserRelatedProfiles or user_related_profiles = Profile.objects.filter(year__in=user.birthday.year) ?

Regards,

Andréas

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Andréas Kühne

unread,
Jan 30, 2018, 2:17:58 AM1/30/18
to django...@googlegroups.com
Hi,

No, year__in should be for a list. For example you can write: Profile.objects.filter(year__in=[2017, 2016, 2015])

so in your case it would be Profile.objects.filter(year=user.birthday.year)

You should checkout how selection works in the django documentation:

Regards,

Andréas

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages