alioguzhan
alioguzhan

Reputation: 7917

django - relations between models

i have two model which are Post and Profile. i am keepin blog datas which are title,body,owner,slug etc in Post. and keepin' user profile settings which are slogan,email,website etc. in Profile

in my index.html page i display user profile infos and post lists in same page. so ; i need to connect these two models each other. when someone goes to 127.0.0.1/blog/username (with or without login) all the data which are belong to user named 'username' must be there.

here is my models.py:

class Profile(models.Model):
    slogan = models.TextField(blank=True)
    twitter = models.CharField(max_length = 100,blank=True)
    web_site = models.CharField(max_length=100,blank=True)
    email = models.CharField(max_length = 100,blank=True)

    def __unicode__(self):
        return self.slogan


class Post(models.Model):
    owner = models.ForeignKey(User)
    title = models.CharField(max_length = 100)
    body = models.TextField()
    bodyPreview = models.TextField() #preview için body alanı
    titlePreview = models.CharField(max_length=100) # preview için title alanı
    slug = AutoSlugField(populate_from='title' ,unique=True)
    posted = models.DateField(auto_now_add=True)
    isdraft = models.BooleanField(default=False)

    def __unicode__(self):
        return self.title

    @permalink
    def get_absolute_url(self):
        return ('view_blog_post',None,{'postslug':self.slug})

and my index view :

def index(request,username):
    post_list = Post.objects.filter(owner__username=username).filter(isdraft=False).order_by("-posted")
    p_index = Paginator(post_list,3) #anasayfa için pagination.[her sayfada 3 post]
    page = request.GET.get('page')

    try:
        indexPag = p_index.page(page)
    except PageNotAnInteger:
        indexPag = p_index.page(1)
    except EmptyPage:
        indexPag = p_index.page(p_index.num_pages)

    ## i need to get user's profile datas here. ??

    return render_to_response('index.html',
                             {'post_list':post_list,'p_index':indexPag,'profile':query},
                             context_instance = RequestContext(request))

Upvotes: 0

Views: 615

Answers (3)

iferminm
iferminm

Reputation: 2059

The most natural way to achieve that is to add a OneToOne relation between your Profile model and the django's User model.

from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User)
    # More fields

class Article(models.Model):
    author = models.ForeignKey(User)
    # More fields

This way, you can access the profile data through a User object. Would be something like this:

user = User.objecets.get(username=username)
profile = user.profile

More info about this, you can read the django model fields documentation here and you can also see this cool answer about the diference between the ForeignKey with unique=True and the OneToOneField

I hope this helps

Upvotes: 0

thikonom
thikonom

Reputation: 4267

I think you should change your Profile model and add a OneToOne relationship to the User model(for more info see here):

class Profile(models.Model):
    user = models.OneToOneField(User)
    ...

class Posts(models.Model):
    author = models.ForeignKey(User)
    ...

and then in your views you can do:

user = get_object_or_404(User, username=username)
post_list = Post.objects.filter(author=user).filter(isdraft=False).order_by("-posted") 

return render_to_response('index.html', 
                           {'post_list': post_list, 'user': user, ...}, ...)

And then in your template you are able to access the user's profile.More here e.g {{user.get_profile.slogan}}

Upvotes: 1

yakxxx
yakxxx

Reputation: 2941

In your view:

def index(request, username):
    user = User.objects.get(username=username)
    return render(request, 'index.html', {'user':user})

In your template:

{{ user.post_set }}

and You will receive list of posts of current user.

Upvotes: 0

Related Questions