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 <[email protected]
> <javascript:>> 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 [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> 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
>>
>> <https://groups.google.com/d/msgid/django-users/dedbfe8b-1013-4844-b142-1e8615a1ef9a%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
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/950d853a-ade3-4d27-8e23-a47c6b70c80e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.