Is there a way to add multiple models to a class view (django)

Hi again! Is there a way to add multiple models to a class view (django)? I’m trying to add two models to a group update view so that when a new group is created the user gets added to the groups userList. Attached is pictures from the model and views (the relevent stuff). For the AllgroupsCreateView class I want to be able to use two models: model = Allgroups, userlist So that when the new group is created the author of the new group can be added to the userlist in the same view. Any ideas? s! (There have been alot of other problems on here that are more complicated than this one. All I want to know is if there is a way to have more than one model for a class view.  do not keep flagging.) Transcribed Image Text: allgroupslviews.py
from .models import Allgroups, userlist,
class AllgroupsCreateView(LoginRequiredMixin, CreateView):
model = Allgroups
template_name = ‘allgroups_new.html’
fields =
(‘title’, ‘body’,’private’,)
login_url = ‘login’
def form_valid(self, form):
form.instance.author = self.request.user
return super(). form_valid(form)
class userlistUpdateView(CreateView):
model = userlist
fields = (‘user’,’allgroups’,)
template_name = ‘userlist_update.html’
success_url = reverse_lazy(‘allgroups_list’)
def test_func (self):
obj = self.get_object ()
return obj.author == self.request.user Transcribed Image Text: allgroupsimodels.py
class Allgroups (models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
date = models.DateTimeField(auto_now_add=True)
private = models.BooleanField(default=False)
discussion = models.Charfield(max_length=255)
author = models.Foreignkey(
get_user_model(),
on_delete=models.CASCADE,
def _str_(self):
return self.title
def get_absolute_ur1(self):
return reverse(‘allgroups_detail’, args=[str(self.id)])
class userlist(models.Model):
allgroups = models.Foreignkey(
Allgroups,
on_delete=models.CASCADE,
related_name = ‘userList’,
user = models.Foreignkey(
get_user_model(),
on_delete=models.CASCADE,
def _str_(self):
return str(self.user)

-25-


Buy plagiarism free, original and professional custom paper online now at a cheaper price. Submit your order proudly with us



Essay Hope