zack namane
zack namane

Reputation: 79

Associate user to a post in Django

hi everyone i'm trying to make a blog and i want to associate a user to post .. and to comment , it's a multi-user blog and i don't know how to do it , any help guys ? !

here is the model file :

from django.db import models
from django.utils import timezone
from django.conf import settings
from django.utils.text import slugify

# Create your models here.
#this is for categories 
class Category(models.Model):
    title=models.CharField(max_length=100,default='')
    def __str__(self):
        return self.title
        #this is where a user can create his own gigs 
class Gigposter(models.Model):

    title=models.CharField(default='',max_length=100,blank=False)
    user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=False)
    categories=models.OneToOneField(Category,on_delete=models.PROTECT,default='',null=False)
    published_at=models.DateTimeField(auto_now_add=True)
    description=models.TextField(default='',max_length=None,blank=False)
    mainphoto=models.ImageField(default='')
    photo=models.ImageField()
    def __str__(self):
        return self.title

#this is where a user can comment and say what he thinks about others work
class Comment_area(models.Model):

    user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=False)
    comment=models.TextField(max_length=None,default='')
    commented_at=models.DateTimeField(auto_now_add=True)

and the views file is empty as you can see :

from django.shortcuts import render

# Create your views here.

Upvotes: 1

Views: 698

Answers (3)

Khaled
Khaled

Reputation: 1

from django.contrib.auth.models import User

class Post(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE)

Upvotes: 0

Ivan K.
Ivan K.

Reputation: 460

If you want to automatically associate a user to a post when working on the admin page, you should redefine save_model method of your model. This method describes everything what should be done when you save your model. In your case you should add something like

class GigposterAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
    obj.user = `request.user`
    super().save_model(request, obj, form, change)

admin.site.register(Gigposter, GigposterAdmin)

to your admin.py. You should also exclude the user field from fieldset in GigposterAdmin. See this for reference.

If you need to identify user in your views, you can always use request.user. In particular, you can pass it as a part of the context for generating a view. Hope this helps.

Upvotes: 1

Wassim Katbey
Wassim Katbey

Reputation: 327

Wouldn't recommend using a OneToOneField here, as it tells Django the user is tied to exactly one comment/post (when, a user is likely to post more than once).

You could use models.ForeignKey:

from django.contrib.auth.models import User

class Gigposter(models.Model):
   # Other properties...
   user = models.ForeignKey(
                    'User'
                     on_delete=models.CASCADE
                 )

Upvotes: 2

Related Questions