i need to auto set the model field "auther = models.ForeignKey(User, on_delete=models.CASCADE)" to the current authenticated user so that any posts created are automatically assigned to the user that created them...
currently with my code the user has to select their own username in a drop down list. i would like to remove this and have that field auto filled out in the back end to prevent users picking the wrong username.
here is my models.py
class Task(models.Model):
SELECT = 'None'
GREEN = 'Green'
AMBER = 'Amber'
RED = 'Red'
PRIORITY = [(SELECT, 'Select Priority'),(GREEN, 'Green'),
(AMBER, 'Amber'),(RED, 'Red'),]
auther = models.ForeignKey(User, on_delete=models.CASCADE,)
priority = models.CharField(max_length=5,choices=PRIORITY,default=SELECT,)
date = models.DateField(auto_now_add=True)
title = models.CharField(max_length=255)
description = models.TextField()
def __str__(self):
return self.title ' | ' str(self.auther)
def get_absolute_url(self):
return reverse('task')
def is_upperclass(self):
return self.PRIORITY in {self.GREEN, self.AMBER}
here is my views.py
class TaskView(ListView):
model = Task
template_name = 'tasks.html'
ordering = ['-id']
class AddTaskView(SuccessMessageMixin,CreateView):
model = Task
template_name = 'add_task.html'
fields = ['priority','title','description']
success_message = " Task was created successfully"
class UpdateTaskView(SuccessMessageMixin, UpdateView):
model = Task
template_name = 'update_task.html'
fields = ['priority','title','description']
success_message = " Task was updated successfully"
class DeleteTaskView(SuccessMessageMixin, DeleteView):
model = Task
template_name = 'delete_task.html'
success_url = '/task/'
success_message = "Task was closed successfully"
ive tried adding my own save_model method but this didnt work as i get the error "IntegrityError at /add_task/ NOT NULL constraint failed: home_task.auther_id"
when i remove the option to pick the user in the form...
that attempt looks like this
class Task(models.Model):
SELECT = 'None'
GREEN = 'Green'
AMBER = 'Amber'
RED = 'Red'
PRIORITY = [(SELECT, 'Select Priority'),(GREEN, 'Green'),
(AMBER, 'Amber'),(RED, 'Red'),]
auther = models.ForeignKey(User, on_delete=models.CASCADE,)
priority = models.CharField(max_length=5,choices=PRIORITY,default=SELECT,)
date = models.DateField(auto_now_add=True)
title = models.CharField(max_length=255)
description = models.TextField()
def __str__(self):
return self.title ' | ' str(self.auther)
def get_absolute_url(self):
return reverse('task')
def is_upperclass(self):
return self.PRIORITY in {self.GREEN, self.AMBER}
def save_model(self, request, obj, form, change):
obj.auther = request.user
super().save_model(request, obj, form, change)
CodePudding user response:
In the CreateView
, you assign it to the instance:
from django.contrib.auth.mixins import LoginRequiredMixin
class AddTaskView(SuccessMessageMixin, LoginRequiredMixin, CreateView):
model = Task
template_name = 'add_task.html'
fields = ['priority', 'title', 'description']
success_message = " Task was created successfully"
def form_valid(self, form):
form.instance.auther = self.request.user
return super().form_valid(form)
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.