modelForm got an unexpected argument 'initial' I am getting this error. please can anyone explain how to solve this ?
Here is my model.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, default=None)
StudentID = models.CharField(max_length=8, blank=False, unique=True)
Branch = models.CharField(max_length=255,choices=Departments,default="CSE")
def __str__(self):
return f'{self.user.username} Profile'
class complaintForm(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
category = models.CharField(max_length=255,choices=complaints,default='Mess')
title = models.CharField(max_length=20)
content = models.CharField(max_length=100)
image = models.ImageField(upload_to='complaint_pics/')
def __str__(self):
return self.title
form.py
class complaintForm(forms.ModelForm):
class Meta:
model = complaintForm
fields = ['title','content','image',]
views.py
class CreateComplaintView(CreateView):
model = complaintForm
form_class = complaintForm
template_name = 'user/post_complaint.html'
success_url = 'success'
You passed your model to the form attribute. This is why ComplaintForm is not a good idea for a model name. You better rename this to Complaint for example:
class Complaint(models.Model):
user = models.ForeignKey(Profile, on_delete=models.CASCADE)
category = models.CharField(max_length=255,choices=complaints,default='Mess')
title = models.CharField(max_length=20)
content = models.CharField(max_length=100)
image = models.ImageField(upload_to='complaint_pics/')
def __str__(self):
return self.title
You will need to construct and run migrations to rename the table at the database side.
Then you thus define your form as:
from app.models import Complaint
class ComplaintForm(forms.ModelForm):
class Meta:
model = Complaint
fields = ['title','content','image',]
Finally in your CreateView, you can use:
from app.models import Complaint
from app.forms import ComplaintForm
class CreateComplaintView(CreateView):
model = Complaint
form_class = ComplaintForm
template_name = 'user/post_complaint.html'
success_url = 'success'
def form_valid(self, form):
form.instance.user = self.request.user.profile
super().form_valid(form)
Note: normally a Django models, just like all classes in Python are given a name in PerlCase, not snake_case, so it should be: Complaint instead of complaint.
Related
I have created a form named as AttendanceForm :
class AttendanceForm(forms.ModelForm):
class Meta:
model = Attendance
fields = '__all__'
These are models
class Employee(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)
eid = models.IntegerField(primary_key=True)
salary = models.IntegerField()
gender = models.CharField(max_length=6, choices=GENDER_CHOICES, default=1)
contactno = models.CharField(max_length=10)
email = models.CharField(max_length=30)
country = models.CharField(max_length=30)
city = models.CharField(max_length=20)
pincode = models.IntegerField()
address = models.CharField(max_length=60)
def __str__(self):
return self.user.first_name + ' ' + self.user.last_name
class Attendance(models.Model):
employee = models.ForeignKey(Employee, on_delete=models.CASCADE, default=1)
attendancedate = models.DateField()
in_time = models.TimeField()
out_time = models.TimeField()
description = models.TextField()
def __str__(self):
return str(self.employee)
view for attendance.
#csrf_exempt
def addattendance(request):
form = AttendanceForm()
emp_list = Employee.objects.all()
if request.method == 'POST':
form = AttendanceForm(request.POST)
if form.is_valid():
form.save(commit=True)
return redirect('employee/detail_attendance')
return render(request, 'employee/addattendance.html', {'form': form, 'emp_list': emp_list})
I tried everything, but I don't know why the data is not saving into the database. Also, models are created fine, and the main thing is that there are no errors coming up.
Please let me know if any changes are required.
I can suggest simple solution with Class-Based-Views:
from django.views.generic.edit import FormView
def AddAttendanceFormView(FormView):
form_class = AttendanceForm
extra_context = {"emp_list": Employee.objects.all()}
success_url = reverse_lazy('employee/detail_attendance')
template_name = 'employee/addattendance.html'
def post(self, *args, **kwargs):
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
return self.form_invalid(form)
Remember, that in urls.py you need to use .as_view() for class based views, like:
path((...), AddAttendanceFormView.as_view())
Also, you will not need #csrf_exempt, just put {% csrf_token %} anywhere inside your form's template.
# models.py
class NewBlank(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
title = models.CharField(max_length=50)
description = models.CharField(max_length=100, blank=True)
blank_on_off = models.BooleanField(default=False)
create_date = models.DateTimeField(auto_now_add=True)
update_date = models.DateTimeField(auto_now=True)
class BlankContent(models.Model):
refer = models.TextField()
memo = models.TextField()
new_blank = models.ForeignKey('NewBlank', on_delete=models.CASCADE, related_name='blankcontent')
# views.py
class BlankDetail(LoginRequiredMixin, DetailView):
model = NewBlank
template_name = 'blank_app/blank_detail.html'
context_object_name = 'blank'
class BlankContentCreate(CreateView):
model = BlankContent
fields = "__all__"
template_name = 'blank_app/new_blank_content_create.html'
def get_success_url(self):
return reverse_lazy('blank_detail', kwargs={'pk': self.object.new_blank.pk})
# urls.py
urlpatterns = [
path('blank/<int:pk>/', BlankDetail.as_view(), name='blank_detail'),
path('new-blank-content/', BlankContentCreate.as_view(), name='blank_content_create'),
]
There is a creativeview in the detail view and I want to create a model in the detailview when I press it. So even if I don't specify the new_blank part, I want it to be filled automatically according to the pk in the detailview, what should I do?
In case you want to perform some extra work in your DetailView, one of the ways to do that would be to override the get_object method.
from django.views.generic import DetailView
class BlankDetail(LoginRequiredMixin, DetailView):
model = NewBlank
template_name = 'blank_app/blank_detail.html'
context_object_name = 'blank'
def get_object(self):
obj = super().get_object()
# do your thing with obj.pk
pk = self.kwargs.get('pk') # in case you want to access the `pk` from URL
Could anyone explain to me how to do it? I've been trying for a while without managing to do it. I am just trying to restrict permissions so that not anyone with the right link can edit/delete a particular object
models.py:
class Items(models.Model):
author = models.ForeignKey(
'auth.User', on_delete=models.CASCADE, null=True)
name = models.CharField(max_length=256)
description = RichTextField(blank=True, null=True)
url = models.URLField(null=True, blank=True)
image = models.ImageField(null=True, blank=True)
class Meta:
ordering = ['name']
def __str__(self):
return self.name
and
views.py
class SiteList(ListView):
model = Items
class SiteUpdate(LoginRequiredMixin, UpdateView):
model = Items
form_class = SiteUpdateForm
success_url = reverse_lazy('portfolio:sitelist')
class SiteDelete(DeleteView):
model = Items
success_url = reverse_lazy('portfolio:sitelist')
You can implement a get_object() (see the inheritance hierarchy) that enforces this.
class MustBeAuthorGetObjectMixin:
def get_object(self):
obj = super().get_object()
if obj.author != self.request.user:
raise PermissionDenied("...")
return obj
# ...
class SiteUpdate(MustBeAuthorGetObjectMixin, LoginRequiredMixin, UpdateView):
# ....
This way, when UpdateView (well, BaseUpdateView) calls self.get_object(), it'll get your version which has the permission check.
I am trying to join two tables and serialize them as an API. I have referred to the docs of the Django rest framework and tried a code. It didn't work. Could not resolve the problem even after trying so many times. I am trying to get a JSON file like
{
'album_name': 'The Grey Album',
'artist': 'Danger Mouse',
'tracks': [
{'order': 1, 'title': 'Public Service Announcement'},
{'order': 2, 'title': 'What More Can I Say'},
{'order': 3, 'title': 'Encore'},
...
],
}
But what I get is
{
'album_name': 'The Grey Album',
'artist': 'Danger Mouse',
}
This is the model file I am using
Model.py
from django.db import models
from django.contrib.auth.models import User
STATUS_CHOICE = (
('simple', 'simple'),
('intermediate', 'intermediate'),
)
class Quiz(models.Model):
quiz_name = models.CharField(max_length=1000)
video_id = models.ForeignKey("youtube.Youtube", on_delete=models.CASCADE)
questions_count = models.IntegerField(default=0)
description = models.CharField(max_length=70, null=True)
created = models.DateTimeField(auto_now_add=True)
slug = models.SlugField()
pass_mark = models.IntegerField()
class Meta:
ordering = ['created']
def __str__(self):
return self.quiz_name
class Category(models.Model):
category = models.CharField(max_length=20, choices=STATUS_CHOICE, default='simple')
quiz_id = models.ForeignKey(Quiz, on_delete=models.CASCADE)
def __str__(self):
return self.category
class Questions(models.Model):
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
question = models.CharField(max_length=1000)
mark = models.IntegerField()
def __str__(self):
return self.question
class Choice(models.Model):
question = models.ForeignKey(Questions, on_delete=models.CASCADE)
choice_1 = models.CharField(max_length=1000)
choice_2 = models.CharField(max_length=1000)
choice_3 = models.CharField(max_length=1000)
choice_4 = models.CharField(max_length=1000)
answer = models.CharField(max_length=1000, default=choice_1)
def __str__(self):
return self.answer
Serializer.py
from rest_framework import serializers
from rest_framework.permissions import IsAuthenticated
from .models import Category, Quiz, Questions, Choice
from django.contrib.auth import authenticate
from django.contrib.auth.hashers import make_password
class QuizSerializer(serializers.ModelSerializer):
class Meta:
model = Quiz
fields = '__all__'
class QuestionsSerializer(serializers.ModelSerializer):
class Meta:
model = Questions
fields = '__all__'
class ChoiceSerializer(serializers.ModelSerializer):
class Meta:
model = Choice
fields = '__all__'
class CategorySerializer(serializers.ModelSerializer):
quiz_name = QuizSerializer(read_only=True)
class Meta:
model = Category
fields = ['id','category','quiz_name']
View.py
from rest_framework import generics, permissions, mixins
from rest_framework.response import Response
from .serializer import CategorySerializer
from .models import Category
class ViewQuiz(generics.ListCreateAPIView):
permission_classes = [
permissions.AllowAny,
]
queryset = Category.objects.all()
serializer_class = CategorySerializer
def list(self, request):
queryset = self.get_queryset()
serializer = CategorySerializer(queryset, many=True)
print(serializer.data)
return Response(serializer.data)
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ['id','category','quiz_id']
def to_representation(self, instance):
response = super().to_representation(instance)
response['quiz_id'] = QuizSerializer(instance.quiz_id).data
return response
This will produce the result you want, I made an change in how the serializer represent the data. I have some of my serializer doing the same, but my views are working a bit different from yours.
Looks like you are trying to get questions serializes in quiz.
To do that you need to:
1. In Questions model include related_name in quiz field:
class Questions(models.Model):
quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE, related_name="questions")
question = models.CharField(max_length=1000)
mark = models.IntegerField()
def __str__(self):
return self.question
In QuizSerializer include questions field and set many to True:
class QuizSerializer(serializers.ModelSerializer):
questions = QuestionsSerializer(source="questions", many=True)
class Meta:
model = Quiz
fields = ("questions", ... other needed fields)
Include source attribute in QuizSerializer in CategorySerializer:
class CategorySerializer(serializers.ModelSerializer):
quiz_name = QuizSerializer(read_only=True, source="quiz_id")
class Meta:
model = Category
fields = ['id', 'category', 'quiz_name']
Your Quiz was not serialized because the relation between Category and Quiz in tables are called quiz_id but your field is called quiz_name, so the framework did not know where it should take quiz, because it was looking at quiz_name relation which does not exist.
I am trying to create a sign up form where the user can specify many categories and languages from a list, but when I signup up choosing one or two objects from the list, I found that the new user is stored with all the objects. It is like I had checked all the objects.
forms.py
class SignUpForm(UserCreationForm, ModelForm):
categories=forms.ModelMultipleChoiceField(queryset=Category.objects.all(),
widget=forms.CheckboxSelectMultiple(), required=True)
languages = forms.ModelMultipleChoiceField(queryset=Language.objects.all(),
widget=forms.CheckboxSelectMultiple(), required=True)
class Meta:
model = User
fields = ['username', 'email','categories', 'languages']
models.py
class User(AbstractUser, models.Model):
password1 = models.CharField(max_length=50)
categories = models.ManyToManyField('Category')
languages = models.ManyToManyField('Language')
def __str__(self):
return self.username
class Category(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
class Language(models.Model):
name = models.CharField(max_length=50)
def __str__(self):
return self.name
views.py
class SignUp(CreateView):
form_class = SignUpForm
success_url = reverse_lazy('index')
template_name = 'registration/signup.html'