Automatically assign model instance to the user who created it. And only that user can edit/delete the instance.

43 views
Skip to first unread message

Jack Zhang

unread,
Oct 24, 2017, 12:07:33 PM10/24/17
to Django users
Let's say I have a model called 'Dogs'.  Users can create instances of Dogs.  Let's say we have a user called User1.  I have 2 questions:

1. When User1 creates an instance of Dogs, how do I automatically assign that instance to User1?  I tried using a model field like this but it doesn't pick the user automatically:

user = models.ForeignKey(User, unique=False)

2. After the instance has been created, how do I make the instance only editable and removable by the user who made it?  Basically, if User1 creates an instance of Dogs, only User1 can edit and delete that instance. 

Thanks.

mohammad k

unread,
Oct 24, 2017, 12:35:15 PM10/24/17
to django...@googlegroups.com
try that in your views
form = Dog_Form(request.POST)
if form.is_valid():
form_save = form.save(commit=False)
form_save.user = request.user # user is logged in
form.save()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/dedbfe8b-1013-4844-b142-1e8615a1ef9a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mohammad k

unread,
Oct 24, 2017, 12:36:02 PM10/24/17
to django...@googlegroups.com
for question 2 :

    def get_queryset(self):  # the user want to edit this post must be owner this post
        post_qs = super(UpdatePost, self).get_queryset()
        return post_qs.filter(user=self.request.user)

Jack Zhang

unread,
Oct 24, 2017, 7:52:30 PM10/24/17
to Django users
Thanks for your response.  This is an off-topic question but I can't figure out how to get 'request' working.  I'm using the CreateView generic class, so there's no space to insert 'request'.  This is what my code looks like.  When I try to run it, it tells me that request is not found.  

class ListingCreateView(LoginRequiredMixin, CreateView):
    model = BuyerListing
    
    form_class = PostListing(self.request.POST)
    if form.is_valid():
        form_save = form.save(commit=False)
        form_save.user = request.user # user is logged in
        form.save()
        
    def get_queryset(self):  # the user want to edit this post must be owner this post
        post_qs = super(PostListing, self).get_queryset()
        return post_qs.filter(user=self.request.user)

On Tuesday, October 24, 2017 at 12:35:15 PM UTC-4, k2527806 wrote:
try that in your views
form = Dog_Form(request.POST)
if form.is_valid():
form_save = form.save(commit=False)
form_save.user = request.user # user is logged in
form.save()
On Tue, Oct 24, 2017 at 7:37 PM, Jack Zhang <valac...@gmail.com> wrote:
Let's say I have a model called 'Dogs'.  Users can create instances of Dogs.  Let's say we have a user called User1.  I have 2 questions:

1. When User1 creates an instance of Dogs, how do I automatically assign that instance to User1?  I tried using a model field like this but it doesn't pick the user automatically:

user = models.ForeignKey(User, unique=False)

2. After the instance has been created, how do I make the instance only editable and removable by the user who made it?  Basically, if User1 creates an instance of Dogs, only User1 can edit and delete that instance. 

Thanks.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

mohammad k

unread,
Oct 25, 2017, 5:52:29 AM10/25/17
to django...@googlegroups.com
class Upload(FormView):
    http_method_names = ['get', 'post']
    template_name = 'public/upload.html'
    success_url = reverse_lazy('upload')
    form_class = UploadForms

    def get(self, request, *args, **kwargs):
        form = self.form_class(user=request.user)
        return render(request, self.template_name, {'form': form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST, request.FILES, user=request.user)
        if form.is_valid():
            data = form.save(commit=False)
            data.user = request.user
            data.save()
            messages.add_message(request, messages.SUCCESS, self.success_message)
            return redirect(self.success_url)
        return render(request, self.template_name, {'form': form})

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

mohammad k

unread,
Oct 25, 2017, 5:52:37 AM10/25/17
to django...@googlegroups.com

mohammad k

unread,
Oct 25, 2017, 5:57:04 AM10/25/17
to django...@googlegroups.com
that your code : 
class CreatePost(CreateView):
    model = Post

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(CreatePost, self).form_valid(form)

Jack

unread,
Oct 25, 2017, 11:55:28 AM10/25/17
to Django users
I tried using FormView but kept running into errors.  I used your second response and it worked!  I don't fully understand the code, but it seems to work flawlessly.

Thank you so much! I own you a big one.

mohammad k

unread,
Oct 25, 2017, 12:37:42 PM10/25/17
to django...@googlegroups.com
when you submited form, form_valid will called in CreateView and give you a instance of user form submited (form parameter in form_valid function) and you set user to logged user (self.request.user)

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages