I'm creating a Django project and I am having troubles using a model that I created for one of my classes *allgroups\models.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_url(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) def get_absolute_url(self): return reverse('allgroups_list') ------------------------------------------------------------------------- *allgroups\views.py* from .models import Allgroups, userList class AllgroupsListView(ListView): model = Allgroups template_name = 'allgroups_list.html'   class AllgroupsDetailView(DetailView): model = Allgroups template_name = 'allgroups_detail.html'   class userListUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView): model = userList fields = ('user',) template_name = 'userlist_update.html' login_url = 'login' def test_func(self): obj = self.get_object() return obj.author == self.request.user ----------------------------------------------------------------------------------- *allgroups\urls.py* from django.urls import path, include from .views import ( AllgroupsListView, AllgroupsDetailView, userListUpdateView, ) urlpatterns = [ path('', AllgroupsListView.as_view(), name='allgroups_list'), path('/', AllgroupsDetailView.as_view(), name='allgroups_detail'), path('/update/', userListUpdateView.as_view(), name='userlist_update'), ] ---------------------------------------------------------------------------------------------------------------- *templates\allgroups_list.html* {% extends 'base.html' %} {% block title %}All Groups{% endblock title %} {% block content %} . . . {{ allgroups.body }} Updatet | . . . ----------------------------------------------------------------------- *templates\userlist_update* {% extends 'base.html' %} {% block content %} Working? {% csrf_token %} {{ form.as_p }} Update {% endblock content %} ------------------------------------------------------------------------------------------------ I'm not sure why I'm unable to use the userList class as a model for my view. When I change the userListUpdateView model = allgroups and fields = ('author'), it works perfectly. I'm able to update the userList in admin. Any help would be greatly appreciated! Thanks!

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

I'm creating a Django project and I am having troubles using a model that I created for one of my classes

*allgroups\models.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_url(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)

def get_absolute_url(self):
return reverse('allgroups_list')

-------------------------------------------------------------------------

*allgroups\views.py*

from .models import Allgroups, userList

class AllgroupsListView(ListView):
model = Allgroups
template_name = 'allgroups_list.html'

 

class AllgroupsDetailView(DetailView):
model = Allgroups
template_name = 'allgroups_detail.html'

 

class userListUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = userList
fields = ('user',)
template_name = 'userlist_update.html'
login_url = 'login'

def test_func(self):
obj = self.get_object()
return obj.author == self.request.user

-----------------------------------------------------------------------------------

*allgroups\urls.py*

from django.urls import path, include
from .views import (
AllgroupsListView,
AllgroupsDetailView,
userListUpdateView,
)

urlpatterns = [
path('', AllgroupsListView.as_view(), name='allgroups_list'),
path('<int:pk>/', AllgroupsDetailView.as_view(), name='allgroups_detail'),
path('<int:pk>/update/', userListUpdateView.as_view(), name='userlist_update'),

]

----------------------------------------------------------------------------------------------------------------

*templates\allgroups_list.html*

{% extends 'base.html' %}
{% block title %}All Groups{% endblock title %}
{% block content %}

.

.

.

<div class="card=body">
<p>{{ allgroups.body }}</p>
<a href="{% url 'userlist_update' allgroups.pk %}">Updatet</a> |

</div>

.

.

.

-----------------------------------------------------------------------

*templates\userlist_update*

{% extends 'base.html' %}

{% block content %}
<h1>Working?</h1>
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<button class="btn btn-info ml-2" type="submit">Update</button>
</form>
{% endblock content %}

------------------------------------------------------------------------------------------------

I'm not sure why I'm unable to use the userList class as a model for my view. When I change the userListUpdateView model = allgroups and fields = ('author'), it works perfectly. I'm able to update the userList in admin.

Any help would be greatly appreciated!

Thanks!

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Multithreading Methods
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education