I want to display a list of items and also a form to add further items at the end of the list. Hence I was thinking of creating a view as something below:
I have come across a class called ListCreateAPIView but I don't want to use django rest framework.
Please let me know your suggestions if and how can I club two generic class based view and how the path in urls.py would look like.
Further how should we refer to the list and create form objects in 'listnadd_task.html'?
<views.py>
from django.views.generic import ListView, CreateView
from .models import Task
from .forms import CreateTask
class ListAndAddView(ListView, CreateView)
model = Task
form_class = CreateTask
context_object_name = 'listnadd_task'
...
Related
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
I'm currently doing my second Django project and I want to know something about views.py
in my first project I had classes that had the field:
model = 'name model'
or
form_class = 'name form'
some of them had both
and now in my second project I have class that has:
form_class = forms.UserCreateForm
How should I know which one should I use - from forms or from models and why this time Django won't let me do:
form_class = UserCreateForm
and needs this 'forms.'
I think you have to take back the bases of Django.
What's the best way to start learning django?
You have to learn what is the difference between a model, a view, a form and a template.
You can import the form itself.
from django.contrib.auth.forms import UserCreationForm
class MyCreateView(CreateView):
form_class = UserCreationForm
Or you can import the forms module, in which case you need to use forms.UserCreationForm.
from django.contrib.auth import forms
class MyCreateView(CreateView):
form_class = forms.UserCreationForm
Personally, I think the first is clearer. It is very common to do from django import forms, which would clash with the second import. You could avoid this clash by importing it as auth_forms:
from django.contrib.auth import forms as auth_forms
class MyCreateView(CreateView):
form_class = auth_forms.UserCreationForm
I have two questions concerning models and forms.
1) What is the best way to create automatically forms for the models?
In the example below I have two models - ModelA and ModelB. I need forms for them - ModelAForm and ModelBForm. They should be defined automatically. I do not want to do it manually, because in the future I will add other models, and all the forms will look the same. I am thinking about creating special decorator for models and use modelform_factory.
from django.db import models
from django.forms import ModelForm
class ModelA(models.Model):
...
class ModelB(models.Model):
...
class ModelAForm(ModelForm):
class Meta:
abstract = ModelA
class ModelBForm(ModelForm):
class Meta:
abstract = ModelB
2) Assuming I am using only ModelForm forms, it is possible to find the form for the model? Example. I have two models ModelA and ModelB, and two forms ModelAForm and ModelBForm. I have instance of ModelA and I would like to identify proper form for this model which I will pass to template - in this case ModelAForm.
Django provides some generic editing views. You only have to provide a model and the view will generate the form automatically. If you want to use the forms to create or update instances you can just use them.
If your really need to create the forms yourself you can use the modelform_factory that these views use themself to create the correct form for your model. But i would first go with the generic views as long as they can be modified to suit your needs.
Just pass it your model and optionally the fields you want. If you omit the fields, all fields will be generated:
from .models import MyModel
from django.forms.models import modelform_factory
my_form = modelform_factory(MyModel, fields=['name', 'age', 'job'])
There's ModelForm in django.
Here's fragment of documentation and link for it.
https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/
class ArticleForm(ModelForm):
class Meta:
model = Article
fields = ['pub_date', 'headline', 'content', 'reporter']
You're probably looking for generic class-based views, where you only pass form_class and model instances, everything else django handles without your help.
Link for them - https://docs.djangoproject.com/en/1.10/ref/class-based-views/
I'm trying to add a field to 'Groups' within the Django admin - for instance, when you create a group in the backend, you define 'Name' and 'Permissions', and I'd like to add a field to that list (CharField). Does this require a new app, or can I extend the Group model in my root models.py?
New to django and python here, so sorry if the question is badly worded.
Here's what I have so far:
#models.py
from django.db import models
from django.contrib.auth.models import Group
class AppDBName(Group):
AppDB = models.CharField(max_length=30)
def __unicode__(self):
return self.AppDB
#admin.py
from django.contrib import admin
from .models import AppDBName
admin.site.register(AppDBName)
You can try to create a new model, which will extend the Django built-in group model. To do this you should link a new model, say GroupExtend, to the original one with a OneToOne field, like it can be done for user (link):
from django.contrib.auth.models import Group
class GroupExtend(models.Model):
group = models.OneToOneField(Group, on_delete=models.CASCADE)
# other fields
You don't need to create new app just override the django admin models.py in to your project and customize it as per your requirement.
https://docs.djangoproject.com/en/1.8/topics/db/models/
The documentation shows
We have a number of fields and we want to let our users filter based on the price or the release_date. We create a FilterSet for this:
import django_filters
class ProductFilter(django_filters.FilterSet):
class Meta:
model = Product
fields = ['price', 'release_date']
Where does this code get placed to create a FilterSet? Is it in models or views? Thanks.
Wherever you want, I mean models.py, views.py or even a new file called filters.py. Because you will use that filter in views.py, so you can import the filters from everywhere in your project.
For me, I think a file filters.py in the app is the best place.
So in your views.py import the filters like this:
from .filters import ProductFilter