i have profile table have foreign key of user.
class profile(models.model): created = models.datetimefield(auto_now_add=true) updated = models.datetimefield(auto_now=true) image = models.charfield(max_length=100) user = models.foreignkey(user)
i have comment table have foreign key of user.
class comment(models.model): created = models.datetimefield(auto_now_add=true) updated = models.datetimefield(auto_now=true) content = models.textfield() user = models.foreignkey(user)
i want query table comment , want image of user in profile table. how can query in effective way in django?.
thanks
if you're using django = <1.4 following best practice:
comment = comment.objects.get(pk=1) comment.user.get_profile().image
following deprecation of native profile
model support (django 1.5+) following still possible:
comment = comment.objects.get(pk=1) comment.user.profile.image
django 1.5+ introduces custom auth models can following:
comment = comment.objects.get(pk=1) comment.user.image
at least should change foreignkey
onetoone
relation user column on profile
, django =< 1.4 expects 1 user profile
associated user
.
references:
django 1.4 get_profile
: https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.models.user.get_profile
django 1.5+ extending user model: https://docs.djangoproject.com/en/1.9/topics/auth/customizing/#extending-the-existing-user-model
Comments
Post a Comment