django join to model with same User -
i want connect post model userimage
can jointly represent post
, corresponding related user image
must displayed.
please tell me how generate query.
views
instance = postmodel.objects.order_by('-updated')
model userimage:
class userimage(models.model): user = models.foreignkey(user,on_delete=models.cascade) profileimage = models.imagefield(upload_to="userprofile/")
model contain post detail:
class postmodel(models.model): title = models.charfield(max_length=100) author = models.foreignkey(user,default=none) body=models.textfield() slug = models.slugfield(unique=true) subject=models.textfield() timestamp = models.datetimefield(auto_now_add=true,auto_now=false) updated = models.datetimefield(auto_now=true,auto_now_add=false)
ideally every user should have 1 profile. so, userprofile
model should connected user
model using onetoonefield
class userimage(models.model): user = models.models.onetoonefield(user, on_delete=models.cascade) profileimage = models.imagefield(upload_to="userprofile/")
you can posts , use below.
posts = postmodel.objects.all().order_by('-updated')
html: post content along title , user image
way.
{% post in posts %} <h1>{{post.title}}</h1> <p>{{post.body}}</p> <img src="{{post.author.userimage.profileimage.url}}" /> {% endfor %}
Comments
Post a Comment