Populating foreign key fields in Django - django

In my first app (course) I'm creating courses. Each course has number of chapters and each chapter has quiz. I'm trying to create quiz using second app (quiz). models.py (quiz) :
class Quiz(models.Model):
coursechapter = models.ForeignKey(CourseChapter)
name = models.CharField(max_length=255, verbose_name=u'Quiz name',)
creator = models.ForeignKey(User)
creation = models.DateField(auto_now_add=True)
def __unicode__ (self):
return self.name
class Question(models.Model):
quiz = models.ForeignKey(Quiz)
text = models.CharField(max_length=255, verbose_name=u'Question\'s text')
class QuestionAnswer(models.Model):
question = models.ForeignKey(Question)
text = models.CharField(max_length=255, verbose_name=u'Answer\'s text')
is_valid = models.BooleanField(default=False)
class UserAnswer(models.Model):
answer = models.ForeignKey(QuestionAnswer)
I have template for creating courses, inside that template i have link (Add chapters) which takes me to another template(view) for creating chapters. Inside i have link to create quiz for that specific chapter. That link leads to url: /quiz/new (using url.py from quiz app) which is represented by view.py (from quiz app) .
Problem is i dont know how to get id of chapter for which I'm creating quiz. Example of chapter url (one before user click Create Quiz) /course/new/chapter/197/ , is it possible to somehow send chapter_id (197) through link or is there any other way?
views.py(Quiz):
class CreateQuizView(CreateChapterView):
model = Quiz
template_name = 'quiz/create_quiz.html'
fields = '__all__'
def dispatch(self, request, *args, **kwargs):
self.pk = kwargs.get('pk')
return super(CreateQuizView, self).dispatch(request, *args, **kwargs)
def get_success_url(self):
return reverse('quiz-list',
kwargs={'pk': Quiz.objects.latest('id').id})
def get_context_data(self, **kwargs):
context = super(CreateQuizView, self).get_context_data(**kwargs)
return context
views.py(Course):
class CreateChapterView(CreateView, GroupRequiredMixin):
model = CourseChapter
template_name = 'course/add_chapter.html'
fields = '__all__'
def dispatch(self, request, *args, **kwargs):
self.pk = kwargs.get('pk')
return super(CreateChapterView, self).dispatch(request, *args, **kwargs)
def get_success_url(self):
return reverse('courses-chapters',
kwargs={'pk': Course.objects.latest('id').id})
def get_context_data(self, **kwargs):
context = super(CreateChapterView, self).get_context_data(**kwargs)
context['chapter'] = CourseChapterForm
context['chapters'] = CourseChapter.objects.all()
context['last'] = Course.objects.latest('id')
context['courses'] = Course.objects.all()
context['action'] = reverse('courses-chapters',
kwargs={'pk': Course.objects.latest('id').id})
context['kw'] = self.kwargs
context['quiz'] = QuizForm()
context['question'] = QuestionForm()
context['answer'] = QuestionAnswerForm
return context
def form_valid(self, form):
self.object = form.save()
# chapters = CourseChapter.objects.filter(course_id=Course.id)
return HttpResponseRedirect(self.get_success_url())
urls.py (main)
url(r'^course/', include('course.urls')),
url(r'^quiz/', include('quiz.urls', namespace="quiz")),
urls (course)
url(r'^new/$', course.views.CreateCourseView.as_view(),
name='courses-new',),
url(r'^new/(?P<pk>\d+)/$', course.views.CreateChapterView.as_view(),
name='courses-chapters'),
url(r'^edit/(?P<pk>\d+)/$', course.views.UpdateCourseView.as_view(),
name='courses-edit',),
url(r'^new/chapter/(?P<pk>\d+)/$', course.views.CreateChapterView.as_view(),
name='chapter-content',),
url(r'^edit/chapters/(?P<pk>\d+)/$', course.views.UpdateChapterView.as_view(),
name='chapters-edit',),
urls (quiz):
urlpatterns = [
url(r'^$', quiz.views.ListQuizView.as_view(),
name='quiz-list',),
url(r'^new/$', quiz.views.CreateQuizView.as_view(),
name='quiz-new',),
]

You mentioned that you have a link for creating quiz in your page where you create chapters. I suggest you create link right there itself.
For eg,
lets say you have course 'Learn Python' which has chapter called Introduction and it has id 7, you can format your url as such /add-quiz/chapter/chapter_id. You will have chapter in the page if you pass it from the view.

Change your url format from /quiz/new to something like /quiz/new/<chapter id>.
You can fetch these chapter id from your create quiz view.

Related

Django: how to filter form field based off foreignkey AND many to many relationship?

Currently, when a user creates a task, they can assign it to all users. I only want them to be able to assign a task based on the members of the project. I feel like the concept I have right now works but I need to replace the ????. Task's assignee has a foreignkey relationship with the user_model. The user_model is also connected with members on a many to many relationship.
projects/models.py
class Project(models.Model):
name = models.CharField(max_length=200)
description = models.TextField()
members = models.ManyToManyField(USER_MODEL, related_name="projects")
tasks/models.py
class Task(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateTimeField()
due_date = models.DateTimeField()
is_completed = models.BooleanField(default=False)
project = models.ForeignKey(
"projects.Project", related_name="tasks", on_delete=models.CASCADE
)
assignee = models.ForeignKey(
USER_MODEL, null=True, related_name="tasks", on_delete=models.SET_NULL
)
tasks/views.py
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
# fields = ["name", "start_date", "due_date", "project", "assignee"]
form_class = TaskForm
def get_form_kwargs(self):
kwargs = super(TaskCreateView, self).get_form_kwargs()
kwargs["user"] = self.request.user
kwargs["project_members"] = ??????????
return kwargs
tasks/forms.py
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ["name", "start_date", "due_date", "project", "assignee"]
def __init__(self, *args, **kwargs):
user = kwargs.pop("user")
project_members = kwargs.pop("project_members")
super(TaskForm, self).__init__(*args, **kwargs)
self.fields["project"].queryset = Project.objects.filter(members=user)
self.fields["assignee"].queryset = Project.objects.filter(
members=?????????
)
Update:
I followed SamSparx's suggestions and changed the URL paths so now TaskCreateView knows which project id. I updated my tasks/views to the following but I get a TypeError: "super(type, obj): obj must be an instance or subtype of type" and it points to the line: form = super(TaskForm, self).get_form(*args, **kwargs) Maybe it has something to do with having a get_form_kwargs and get_form function? I kept my existing features for the custom form such as when a user creates a task, they can only select projects they are associated with.
Views.py updated
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
form_class = TaskForm
def get_form_kwargs(self):
kwargs = super(TaskCreateView, self).get_form_kwargs()
kwargs["user"] = self.request.user
return kwargs
def get_form(self, *args, **kwargs):
form = super(TaskForm, self).get_form(*args, **kwargs)
form.fields["assignee"].queryset = Project.members.filter(
project_id=self.kwargs["project_id"]
)
def form_valid(self, form):
form.instance.project_id = Project.self.kwargs["project_id"]
return super(TaskCreateView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy("list_projects")
I have also tried to update the forms.py with the following but get an error that .filter cannot be used on Many to Many relationships.
Updated forms.py
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ["name", "start_date", "due_date", "project", "assignee"]
def __init__(self, *args, **kwargs):
user = kwargs.pop("user")
super(TaskForm, self).__init__(*args, **kwargs)
self.fields["project"].queryset = Project.objects.filter(members=user)
self.fields["assignee"].queryset = Project.members.filter(
project_id=self.kwargs["project_id"]
)
Another thing I have tried is to go back to my first approach now that I have the url paths: tasks/create/(project_id)
Views.py
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
form_class = TaskForm
def get_form_kwargs(self):
kwargs = super(TaskCreateView, self).get_form_kwargs()
kwargs["user"] = self.request.user
kwargs["project_id"] = Project.objects.all()[0].members.name
# prints to auth.User.none
return kwargs
I feel like if the kwargs["project_id"] line can be changed to getting list of members of whatever project with the ID in the URL, then this should solve it
Forms.py
class TaskForm(ModelForm):
class Meta:
model = Task
fields = ["name", "start_date", "due_date", "project", "assignee"]
def __init__(self, *args, **kwargs):
user = kwargs.pop("user")
project_id = kwargs.pop("project_id")
super(TaskForm, self).__init__(*args, **kwargs)
self.fields["project"].queryset = Project.objects.filter(members=user)
self.fields["assignee"].queryset = Project.objects.filter(
members=project_id
)
The problem here is that your task doesn't know what members are relevant to include as assignees until you have chosen the project the task belongs to, and both project and assignee are chosen in the same form, so Django doeesn't know who is relevant yet.
The easiest way to handle this is to ensure the call to create a task is associated with the project it is going to be for - eg,
Update your URLs to handle the project ID
Path('create-task/<int:project_id>', TaskCreateView.as_view(), name='create_task')
Update your view
class TaskCreateView(LoginRequiredMixin, CreateView):
model = Task
template_name = "tasks/create.html"
# fields = ["name", "start_date", "due_date", "assignee"]
#NB: I have remove project from the field list, you may need to do the same in your form as it is handled elsewhere
form_class = TaskForm
def get_form(self, *args, **kwargs):
form = super(TaskCreateView, self).get_form(*args, **kwargs)
form.fields['assignee'].queryset = Project.members.filter(project_id = self.kwargs['project_id'])
Return form
def form_valid(self, form):
form.instance.project_id = project.self.kwargs['project_id']
return super(TaskCreateView, self).form_valid(form)
Add links
Create Task for this project
This will create a link on the project details page, or underneath the project in a listview to 'create task for this project', carrying the project informaton for the view via the URL. Otherwise you will have to get into some rather more complex ajax calls that populate the potential assignees list based on the selection within the project dropdown in a dynamic fashion

One view for two models with redirect

I have Django app with the following model:
class Author(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
This is now using simple generic view:
class AuthorDetail(DetailView):
model = Author
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# some additional context thingies
return context
and with the following URL configuration:
path('author/<slug:slug>/', AuthorDetail.as_view(), name='author-detail'),
Now I want to introduce simple aliases for authors, so for example instead of /author/william-shakespeare I can reach the page also as /author/ws or /author/my-favourite-author.
I came up with this idea (I know that destination could be key to Author, but that (I think) would not change much in the case):
class AuthorAlias(models.Model):
slug = models.SlugField(max_length=200, unique=True)
destination = models.CharField(max_length=200)
So to achieve the redirect I came up with the following in the view:
def get(self, request, **kwargs):
slug = kwargs['slug']
try:
self.object = self.get_object()
except Http404:
self.object = get_object_or_404(AuthorAlias, slug=slug)
return redirect(reverse('author-detail', args=[self.object.destination]))
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
Everything seems to be working fine, but I was wondering it there is a better approach and if this approach can cause any issues I'm not seeing?
The problem here is that destination does not per se refer to a real author. You can alter this by using a ForeignKey instead:
class AuthorAlias(models.Model):
destination = models.ForeignKey('Author', on_delete=models.CASCADE)>
slug = models.SlugField(max_length=200, unique=True)
In the Author class it might be better to implement the get_absolute_url(…) method [Django-doc] to define a canonical url:
from django.urls import reverse
class Author(models.Model):
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=200, unique=True)
def get_absolute_url(self):
return reverse('author-detail', kwargs={'slug': self.slug})
Now we can implement the DetailView with:
from django.http import Http404
from django.shortcuts import get_object_or_404, redirect
class AuthorDetail(DetailView):
model = Author
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# some additional context thingies
return context
def get(self, request, *args, **kwargs):
try:
self.object = self.get_object()
except Http404:
return redirect(get_object_or_404(
Author,
authoralias__slug=self.kwargs['slug']
))
context = self.get_context_data(object=self.object)
return self.render_to_response(context)

Django class based views: Posting form data returns 302 Found status code

I'm using django's generic class based view CreateView to upload images to a book. Here's the code:
# models.py
class Upload(models.Model):
image = models.ImageField(upload_to=get_upload_path, help_text='Image to process')
uploader = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='uploader')
language = models.ForeignKey(Language, models.CASCADE)
book = models.ForeignKey(Book, models.CASCADE)
def __str__(self):
return str(os.path.split(self.image.name)[-1].split('_', 1)[-1])
#models.permalink
def get_absolute_url(self):
return ('book:upload_new', (self.book.id,)) # upload_new is linked to the view below
def save(self, *args, **kwargs):
super(Upload, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
self.image.delete(False)
super(Upload, self).delete(*args, **kwargs)
# views.py
#method_decorator(login_required, name='dispatch')
class PictureCreateView(CreateView):
model = Upload
fields = ("image",)
book_id = None
def dispatch(self, *args, **kwargs):
# book_id is passed as a URL parameter
self.book_id = self.kwargs['book_id']
return super().dispatch(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super(PictureCreateView, self).get_context_data(**kwargs)
context['book_id'] = self.book_id
return context
def form_valid(self, form, **kwargs):
book = Book.objects.get(id=self.book_id)
form.instance.book = book
form.instance.language = book.language
form.instance.uploader = self.request.user
self.object = form.save()
# Running the command below prints
# <TemplateResponse status_code=200, "text/html; charset=utf-8">
# implying the upload was successful
#
# print(self.render_to_response(context=self.get_context_data()))
return super(PictureCreateView, self).form_valid(form)
def form_invalid(self, form):
print(form.errors)
data = json.dumps(form.errors)
return HttpResponse(content=data, status=400, content_type='application/json')
When I'm trying to upload an image, I get a 302 Found error for this request (as seen in the my browser's dev tools) implying the page has moved temporarily. Apart from the headers, I'm unable to see a preview or response for this request. The upload was successful though (I checked in the admin page).
Someone had this issue before and the answer pointed out an error in the URL conf. However, the issue wasn't fully resolved and the OP still got a 302 status code.
What could be the reason for this?
By default form_valid method redirects with 302 code to the success_url (which can be generated dynamically by overriding get_success_url) . So this is the normal behavior and I think a good practice. So I would not change it.
But if you really want to, you can return any other response code, for example : a 200 code with similar content than the get
# views.py
#method_decorator(login_required, name='dispatch')
class PictureCreateView(CreateView):
...
def form_valid(self, form, **kwargs):
book = Book.objects.get(id=self.book_id)
form.instance.book = book
form.instance.language = book.language
form.instance.uploader = self.request.user
self.object = form.save()
context = self.get_context_data()
# You may have check and modify your context for a correct page
# May be: add new things
context["success"] = True
return self.render_to_response(context))

Render Foreign Key in template as Checkboxes instead of Select in Django

Say we have this Model:
class Event(models.Model):
name = models.CharField('Nome do evento', max_length=50)
code = models.CharField('Código de entrada', max_length=10)
artists_list = models.ForeignKey(ListGroup, on_delete=None,
related_name='lists_names', null=True)
and this View
class HomeView(LoginRequiredMixin, TemplateView):
template_name = 'home.html'
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context['form'] = CreateEventForm(self.request.POST or None)
context['defaultTitle'] = 'Novo Evento'
context['formTitle'] = 'Criar'
return context
def post(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
form = context['form']
print(form)
if form.is_valid():
form.save()
return self.render_to_response(context)
and this Form
class CreateEventForm(forms.ModelForm):
class Meta:
model = Event
fields = ('name', 'code', 'artists_list',)
Everything works great, but I would like to be able to select multiple entries that my Foreign key will retrieve. So I would like to render each entry as a checkbox instead of a select. How can I achieve that? I already searched a lot and only found about general charfields, nothing about Foreing Key
Here's how its rendering
A foreign key can't point to multiple entries. If you want that, you should use a ManyToManyField.

How to populate a field in a django model

I have a Django project in which I have a view subclassed from the Django CreateView class. This view is used to upload a file to the server, and uses an UploadedFile model which I have created. The UploadedFile also needs to be associated with a project.
The project id is passed in as part of the URL: (r'^projects/(?P<proj_key>\d+)/$', UploadedFileCreateView.as_view(), {}, 'upload-new')
The problem is that I am not sure where the appropriate place is to associate this key with my model. Is there a method of CreateView or one of its ancestors that I should override that creates the model, or can this be done anywhere in my code in one of the methods I already override (this feels hacky though).
Furthermore, the project attribute of my UploadedFile is defined as a ForeignKey of type Project. How do I get the Project to associate with it?
Here is my model definition:
class Project(models.Model):
"""This is a project that is owned by a user and contains many UploadedFiles."""
name = models.CharField(max_length=200)
class UploadedFile(models.Model):
"""This represents a file that has been uploaded to the server."""
STATE_UPLOADED = 0
STATE_ANNOTATED = 1
STATE_PROCESSING = 2
STATE_PROCESSED = 4
STATES = (
(STATE_UPLOADED, "Uploaded"),
(STATE_ANNOTATED, "Annotated"),
(STATE_PROCESSING, "Processing"),
(STATE_PROCESSED, "Processed"),
)
status = models.SmallIntegerField(choices=STATES,
default=0, blank=True, null=True)
file = models.FileField(upload_to=settings.XML_ROOT)
project = models.ForeignKey(Project)
def __unicode__(self):
return self.file.name
def name(self):
return os.path.basename(self.file.name)
def save(self, *args, **kwargs):
if not self.status:
self.status = self.STATE_UPLOADED
super(UploadedFile, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
os.remove(self.file.path)
self.file.delete(False)
super(UploadedFile, self).delete(*args, **kwargs)
Here is my view definition:
class UploadedFileCreateView(CreateView):
model = UploadedFile
def form_valid(self, form):
logger.critical("Inside form_valid")
self.object = form.save()
f = self.request.FILES.get('file')
data = [{'name': f.name,
'url': settings.MEDIA_URL + "files/" + f.name.replace(" ", "_"),
'project': self.object.project.get().pk,
'delete_url': reverse('fileupload:upload-delete',
args=[self.object.id]),
'delete_type': "DELETE"}]
response = JSONResponse(data, {}, response_mimetype(self.request))
response['Content-Disposition'] = 'inline; filename=files.json'
return super(UploadedFileCreateView, self).form_valid(form)
def get_context_data(self, **kwargs):
context = super(UploadedFileCreateView, self).get_context_data(**kwargs)
return context
You could do it right where you are calling form.save(). Just pass commit=False so that it won't save it to the db until you add the project id. For example:
self.object = form.save(commit=False)
self.object.project_id = self.kwargs['proj_key']
self.object.save()
Just make sure your form excludes the project field.
EDIT: to exclude the field, add an excludes variable to the form meta class:
class UploadedFileForm(forms.ModelForm):
class Meta:
model = UploadedFile
excludes = ('project',)