Access model name in Django CreateView from template - python

I'm using the generic CRUD views in Django 1.6, e.g.:
class KanriCreateView(CreateView):
template_name = 'generic_form.html'
class KanriUpdateView(UpdateView):
template_name = 'generic_form.html'
etc.
N.B. These are classes used as a base class which I subclass inside views.py files throughout the project.
In order to keep DRY I'm writing a generic form template for all create/update views.
For update views, I have access to object in the template, which is the instance I am updating. I then use object.__class__.__name__ (via a custom filter) to get the name of the class (so I can have automatically generated custom buttons like "Add User", "Add Role".etc so the forms look less...generic.
Of course, when I'm using my template in CreateView, object does not exist (as it has not been created), so my custom buttons.etc do not work, and I get a VariableDoesNotExist exception.
Does Django provide the class somewhere so I can use it in the template?

The name of your first view should be different, e.g. KanriCreateView
It might help you to get the name of the view class: {{ view.class.name }}
If you have access to the view class (which is provided by default by the ContextDataMixin) you can access the model attribute of the view class and get the name of the model: {{ view.model.__name__ }}
Cheers

If you're using a ModelForm for your CreateView, this doesn't quite work. This is because you're not specifying
model = MyModel
but instead you're specifying
form_class = MyModelForm
so what you can do instead is
from django.contrib.admin.utils import model_ngettext
model_ngettext(self.form_class._meta.model, 1)

I propose a solution updated for Django 2 and 3: retrieve the model verbose name from the ModelForm associated to the CreateView.
class YourCreateView(CreateView):
form_class = YourModelForm
def get_context_data(self, **kwargs):
"""Add the models verbose name to the context dictionary."""
kwargs.update({
"verbose_name": self.form_class._meta.model._meta.verbose_name,})
return super().get_context_data(**kwargs)
Now you can use {{ verbose_name }} inside your template.
Please, remark the double _meta in the code snippet above: the first is meant to access the model from the ModelForm, while the second accesses the verbose name of the Model.
As with internationalization, remark that if your model uses ugettext as shown below, then the verbose name will automatically be translated in the template.
from django.utils.translation import ugettext_lazy as _
class MyModel(models.Model):
class Meta:
verbose_name = _("your verbose name")

Related

How to pass additional arguments to class view in django?

def edit_course(request,course_id):
course=Courses.objects.get(id=course_id)
return render(request,"hod_template/edit_course_template.html",{"course":course,"id":course_id})
The code above shows a view in django that is defined as a function. I want to recreate the same view as a class based view in django but I couldn't pass in an additional argument such as course_id into the class based view as it shows an error. Can someone tell me how to recreate that above function into a class based view ?
You can make a DetailView with 'course_id' as pk_url_kwarg. An equivalent DetailView is:
from django.views.generic import DetailView
class EditCourseView(DetailView):
model = Courses
pk_url_kwarg = 'course_id'
context_object_name = 'course'
template_name = 'hod_template/edit_course_template.html'
Note: normally a Django model is given a singular name, so Course instead of Courses.
Possible duplicate of URL-parameters and logic in django...
You can access your course_id via
self.kwargs['course_id']
Use it inside class-based view functions

adjust forms that are generated by django generic editing views

I am trying to understand the process of generating generic form views in django. I have a generic view class with just
class BookUpdate(UpdateView):
model = Book
fields = [ 'name',
'pages',
'categorys'
]
which automatically generates a working html form from my model data. But now, I want to modify the field that is shown for categorys, is there any way to do this, or do I have to create a complete working BookForm class and custom BookUpdate class? Here its just 3 fields, but in my real case there are maybe 15 fields that I would need to code by myself, just because of a tiny change in the category field.
Cant I just overwrite the single field, using any class method?
You can either specify fields or form_class in your generic class-based view. With fields, Django will use a modelform_factory to generate the form. There's not much you can customise then.
You should create a BookForm class so that you can customise the fields. In your BookUpdate view, you only need to remove fields and add form_class = BookForm. Here I'm customising the widget for categorys and overriding the form field for pages:
def BookUpdate(UpdateView):
model = Book
form_class = BookForm
def BookForm(ModelForm):
pages = MyCustomPagesField()
class Meta:
model = Book
fields = '__all__'
widgets = {'categorys': MyCustomWidget()}
Note that you don't have to specify all fields, you can use "__all__" to have all fields or you can set exclude = [<list fields to exclude>] to just exclude a couple.
You don't have to code the fields yourself. But there is a small amount of work to do, as there isn't a method to override.
What you need to do is define a custom form. Since that will be a ModelForm, it will use the same logic to automatically create its fields based on the model. You can then override the definition of one of them.
class BookForm(forms.ModelForm):
categorys = forms.ModelMultipleChoiceField(custom_attributes_here...)
class Meta:
model = Book
fields = ["name", "pages", "categorys"]
And now tell your view to use that form:
class BookUpdate(UpdateView):
form_class = BookForm

Django 1.9 get kwargs in class based view

I was wondering if there is a way to get the kwargs directly in a class based view. I know this can be done in functions inside the class, but I'm having problems when I try this:
views.py
class EmployeesUpdateStudies(UpdateView):
form_class = form_ES
model = EmployeePersonal
template_name = 'employeesControll/employees_studies_update_form.html'
success_url = reverse('employee-details', kwargs={'pk': kwargs.get('pk')})
My url is the following
url(r'^employees/detalles/(?P<pk>[0-9]+)/$', login_required(views.EmployeeDetails.as_view()), name='employee-details')
Alasdair's answer solves your problem. You can however define a get_absolute_url method for your EmployeePersonal model which will act as the success_url for your view:
You don’t even need to provide a success_url for CreateView or
UpdateView - they will use get_absolute_url() on the model object
if available.
You'll use self.id in the get_absolute_url method for the model objects primary key.
Reference:
Model Forms
You can't use kwargs in success_url, because when Django loads the class when the server starts, it doesn't have access to the request. Override the get_success_url method instead.
def get_success_url(self)
return reverse('employee-details', kwargs={'pk': self.kwargs['pk']})

Override a form in Django admin

In Django admin I want to override and implement my own form for a model (e.g. Invoice model).
I want the invoice form to have auto-fill fields for customer name, product name and I also want to do custom validation (such as credit limit for a customer). How can I override the default form provided by Django admin and implement my own?
I am new to Django, I appreciate any pointers.
You can override forms for django's built-in admin by setting form attribute of ModelAdmin to your own form class. See:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin
It's also possible to override form template - have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options
If you're looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects
How to override a form in the django admin according to the docs:
from django import forms
from django.contrib import admin
from myapp.models import Person
class PersonForm(forms.ModelForm):
class Meta:
model = Person
exclude = ['name']
class PersonAdmin(admin.ModelAdmin):
exclude = ['age']
form = PersonForm

Django ModelForm with User data in Generic View

I have a model with a foreign key to group (the other fields don't matter):
class Project(models.Model) :
group = models.ForeignKey(Group)
...
I have a model form for this model:
class AddProjectForm(forms.ModelForm):
class Meta:
model = Project
fields = ["group","another"]
In my urls, I am using this in a generic view:
(r'^$', create_object, {'form_class':AddProjectForm, 'template_name':"form.html", 'login_required':True, 'extra_context':{'title':'Add a Project'}}),
That all works, but I want to have the group field display only the groups that the current user belongs to, not all of the groups available. I'd normally do this by passing in the user to the model form and overriding init if I wasn't in a generic view. Is there any way to do this with the generic view or do I need to go with a regular view to pass in that value?
This is gonna look dirty, since the generic view instantiates the form_class with no parameters. If you really want to use the generic_view you're gonna have to generate the class dynamically :S
def FormForUser(user):
class TmpClass(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(TmpClass, self).__init__(*args, **kwargs)
self.fields['group'].queryset = user.group_set.all()
class Meta:
model = Project
fields = ['group', 'another']
Then wrap the create object view
#login_required # Only logged users right?
def create_project(request):
user = request.user
form_class = FormForUser(user)
return create_object(request, form_class=form_class, ..... )
My recommendation is to write your own view, it will give you more control on the long term and it's a trivial view.
No, you'll need to make a regular view. As can be seen by looking at the source code for create_object(), there's no functionality to pass in extra parameters to the modelform (in django 1.2):
http://code.djangoproject.com/svn/django/branches/releases/1.2.X/django/views/generic/create_update.py

Categories