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? Thanks! (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. Please do not keep flagging.)
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? Thanks!
(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. Please do not keep flagging.)
![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](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F9137b2db-2b50-436a-a0a6-56762b4a8751%2Fa63959d6-fb9c-4f07-94bd-1da0207e007f%2Fge63rpy_processed.png&w=3840&q=75)
![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)](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F9137b2db-2b50-436a-a0a6-56762b4a8751%2Fa63959d6-fb9c-4f07-94bd-1da0207e007f%2Fscesj98_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)