1

So I have a class, Y, that is a profile extending User.

I have another class, X, that has a one-to-one relationship with class Y:

class X(models.Model):
    y = models.ForeignKey('class Y')

Given that I have access to a user object, how do I get to the X associated with it?

fox
  • 15,428
  • 20
  • 55
  • 85

1 Answers1

0

You would need to follow something along these lines.

#your user object
user = User.objects.get(name="John Doe")

#returns all x objects related to the user
#i say all objects because using a foreign key is a one to many relationship, not necessarily a one to one relationship
X_objects = user.x_set.all()

#if you need to reduce it down to one object (assuming you do assign it to more than one)
X_object = user.x_set.filter(some_field__someCondition = 'something');
g19fanatic
  • 10,567
  • 6
  • 33
  • 63
  • Does the existence of the Y class complicate this at all? – fox Nov 18 '11 at 18:21
  • Give it a try and let me know what happens. If it doesn't work, give us what it does do and we'll work from there – g19fanatic Nov 19 '11 at 23:33
  • I actually got this working by setting up three variables using three different lookups. Not elegant, but it did the trick. – fox Nov 20 '11 at 15:41