On Dec 28, 2006, at 10:19 AM, Trey wrote:
class Post(models.Model):
postId = models.AutoField(primary_key = True)
userId = models.ForeignKey(User, db_column = 'userId')
title = models.CharField(maxlength = 100)
class User(models.Model):
userId = models.AutoField(primary_key = True)
alias = models.CharField(maxlength = 100)
AFAIK, Django automtically creates an auto-incrementing primary key,
so you don't need to explicitly specify one in models unless you want
something else to be a primary key.
All I want to do is select a Post with a PK and have it return a User
model as well. If not that, then some way I can get the same
information. If someone could illuminate the standard way of doing
this
I would be very grateful.
It should work as indicated in that example in the doc.
If you have:
class User(models.Model):
alias = models.CharField(maxlength=100)
class Post(models.Model):
title = models.CharField(maxlength=100)
user = models.ForeignKey(User)
You should be able to do:
user1 = User(alias='bob')
user1.save()
post1 = Post(title='blah', user=user1)
post1.save()
And then to access the related user of post1:
post1.user.alias
which should spit out 'bob'
(I haven't tested any of this, but it's basically a variation of the
sample page at: http://www.djangoproject.com/documentation/models/
many_to_one/ )
---
David Zhou
[EMAIL PROTECTED]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---