I like the idea in Django Admin of having a SAVE and add another
button in the create view. The issue I’m facing is quota on the number of instances a user is allowed to create for a number of classes.
For example, a user is allowed to add 8 tags by default. In views.py TagListView I have the following code to keep track of the number of tags a user created:
class TagListView(PermissionRequiredMixin, ListView):
...
def get_queryset(self):
queryset = models.Tag.objects.filter(node_id=self.request.user.node_id)
self.counter = len(queryset)
return queryset
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
...
quota = list_view_quota['tag']
if self.counter >= quota:
context['add_btn'] = False
messages.info(self.request, f"The maximum number of tags in your account is {quota}, the actual number is {self.counter}")
else:
context['add_btn'] = True
return context
In the template:
{% if add_btn %}
<input type="submit" class="btn btn-primary btn-sm" name="_addanother" value="SAVE and add another" role="button"></input>
{% endif %}
And in views.py
class TagAddView(PermissionRequiredMixin, SuccessMessageMixin, CreateView):
...
def get_success_url(self):
if '_addanother' in self.request.POST:
return reverse('node-tag-add')
else:
return super().get_success_url()
The first question is: if the user comes from the TagListView, do I need to check if the actual number of tags is less than the allowed number of tags, I guess it’s possible to reload the TagAddView, bypassing the TagListView.
Then, if the user clicks SAVE and add another
I need to add one to the counter
I set in TagListView. How do I get the counter
from the TagListView to the TagAddView
Kind regards,
Johanna